0

ファイルから読み取り、異なる配列に格納するためのコーディングが必要です!!!

例:

ポール 23 54

ジョン 32 56

私の要件は次のとおりです。paul,john文字列配列と23,321つの整数配列に格納する必要があります。同様54,56に、別のint配列でも。

ファイルから入力を読み取って印刷しますが、3 つの異なる配列に保存できません。

int main()
{
    string name;
    int score;
    ifstream inFile ;
     
    inFile.open("try.txt");
    while(getline(inFile,name))
    {
        cout<<name<<endl;
    }
    inFile.close();     
    
}

これを行うためのいくつかのロジックを親切に提案してください。本当に感謝しています... !!!

4

2 に答える 2

0

あなたはプログラミングが初めてだと思いますか?それとも C++ は初めてですか? そのため、開始するためのサンプル コードを提供しました。:)

#include <string>;
#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<string> name;
    vector<int> veca, vecb;
    string n;
    int a, b;

    ifstream fin("try.txt");
    while (fin >> n >> a >> b) {
       name.push_back(n);
       veca.push_back(a);
       vecb.push_back(b);
       cout << n << ' ' << a << ' ' << b << endl;
    }
    fin.close()

    return 0;
}
于 2013-09-24T01:48:56.100 に答える
0

次のコードを試すことができます。

#include <string>
#include <vector>

#include <iostream>
#include <fstream>

int main()
{
    std::string fileToRead = "file.log";
    std::vector<int> column2, column3;
    std::vector<std::string> names;
    int number1, number2;
    std::string strName;

    std::fstream fileStream(fileToRead, std::ios::in);
    while (fileStream>> strName >> number1 >> number2)
    {
       names.push_back(strName);
       column2.push_back(number1);
       column3.push_back(number2);
       std::cout << "Value1=" << strName
           << "; Value2=" << number1
           << "; value2=" << number2
           << std::endl;
    }
    fileStream.close();
    return 0;
}

アイデアは、ファイルの最初の列を文字列 (strName) に読み取り、それをベクトル (names) にプッシュすることです。同様に、2 番目と 3 番目の列は最初に number1 と number2 に読み込まれ、次にそれぞれ column1 と column2 という名前のベクトルにプッシュされます。

実行すると、次の結果が得られます。

Value1=paul; Value2=23; value2=54
Value1=john; Value2=32; value2=56
于 2013-09-24T02:29:27.253 に答える