1

学生の記録を含むが異なる文字で区切られたファイルから読み取る方法はありますか? 私のテキストは以下の通りです:

125 {John_BROWN_Biology} {100_81}
245 {Amanda_SMITH_Chemistry} {83_74}
436 {Michael_CLARK_Calculus} {35_48}

平均を計算して昇順で並べられるように、別々の配列に格納する必要があります。次のように C++ でコードを記述しました。

int rank;
char st_info[SIZE];
int grades[SIZE];
bool success1 = false;
bool success2 = false;
bool success3 = false;

ifstream inFile;
inFile.open("records.txt");
string line;
int i=0, j=0;
while (!inFile.getline(rank, 30, '{').eof){
    inFile.getline(st_info, SIZE, '_');
    inFile.getline(words_array, SIZE, '_');
}
while (!success1)
{
    getline(inFile,Line,'{'); // get values for info array
    stringstream iss1; 
    iss1 << Line; //Put the string into a stream
    iss1 >> rank; //To send data as an int.
    cout << "STUDENT NUMBER:" << rank << "  ";
    while (!success2){
    // for the info in {} part
        getline(inFile,Line,'_'); 
        stringstream iss; 
        iss << Line; 
        iss >> st_info[i];
        cout <<"STUDENT INFO:" << st_info[i] << "   ";
        i++;
        if (getline(inFile,Line,'}')){
            stringstream iss2; 
            iss2 << Line; 
            iss2 >> st_info[i];
            cout << st_info[i] << " ";
            i=0;
            success2 = true;
        }
    }
    getline(inFile,Line,'{'); 
    stringstream iss3; 
    iss3 << Line; 
    iss3 >> grades[j]; 
    cout << "GRADES: "<< grades[i] << " ";
    j++;
    while (!success3){
        getline(inFile,Line,'_');
        stringstream iss4; 
        iss4 << Line;
        iss4 >> grades[j];
        cout << grades[i] << "  ";
        j++;
        if (getline(inFile,Line,'}')){
            stringstream iss5; 
            iss5 << Line;
            iss5 >> grades[j];
            cout << grades[i] << "  ";
            j=0;
            success3 = true;
        }
    }
} 

しかし、出力として私は得ています:

1 J 100
2 A 100
4 M 100

修正しようとしましたが、悪化します。助けてくれる人はいますか?前もって感謝します。

4

1 に答える 1

3

アプローチとコードにはいくつかの問題があります。

  • istream::getline()整数を読み取ることができません。char 配列の配列のみを読み取ることができます。
  • eofは関数であり、プロパティではありません
  • << >>データを解析するために混合する方法stringstreamは最適ではありません。
  • >>st_info[i]文字列ストリームから単一の文字を抽出すると、既存の情報が上書きされます 。
  • そして確かに他の問題。

したがって、ファイルを 1 行ずつ読み取り、stringstream を使用して各行を個別に解析する次のスケルトンを使用することをお勧めします。char の配列ではなく、非メンバー バリアントのみを使用してgetline()文字列を読み取ることに注意してください (これにより、バッファー オーバーフローを考える必要がなくなります)。

...
char delim;  
int rank;
string names, grades;
string line;

while (getline(inFile, line))   // read line by line 
{
    stringstream sst{line};     // then parse the line using a string stream

    sst>>rank;                  // read an integer
    sst>>delim;                 // skip white and read a single char 
    if (delim!='{') {
        cout<<"Error on line: { expected for name instead of "<<delim<<endl; 
        continue;   // next line, please !! 
    }
    getline(sst,names, '}');  // TO DO: error handling
    sst>>delim; 
    if (delim!='{') {
        cout<<"Error on line: { expected for grades instead of "<<delim<<endl; 
        continue;   // next line, please !! 
    }
    getline(sst,grades, '}');  // TO DO: additional error handling

    cout << rank<<" "<<names<<" "<<grades<<endl; // temporary:  

    // TO DO: parse names and grades by using further stringstreams 
} 

オンラインデモ

単純な解析アプローチを使用したことに注意してください。文字を読み取り、予想される開始文字と一致することを確認し、使用getline()して終了文字まで読み取ります (後者は消費されますが、文字列から除外されます)。{...}これにより、フォーマット にネストすることはできません。

于 2016-11-01T22:15:54.850 に答える