C ++:从具有多个定界符的txt文件中读取

C++: reading from txt file with multiple delimeter

有没有办法读取包含学生记录但由不同字符分隔的文件?我的文字如下:

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

试图修复,但情况变得更糟。谁能帮忙?提前致谢。

您的方法和代码存在几个问题:

  • istream::getline() 无法读取整数。它只能读入一个char数组的数组。
  • eof 是函数而不是 属性
  • 您将 << >>stringstream 混合使用来解析数据的方式不是最佳的。
  • >>st_info[i] 从字符串流中提取单个字符,这将覆盖现有信息。
  • 当然还有其他问题。

因此,我建议您使用以下框架,逐行读取文件,并使用字符串流分别解析每一行。请注意,我只使用 getline() 的非成员变体来读取字符串而不是 char 数组(这使我不必考虑缓冲区溢出):

...
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 
} 

Online demo

请注意,我使用了一种简单的解析方法:读取一个字符并检查它是否与预期的开始字符匹配,然后使用 getline() 读取直到结束字符(后者被消耗但从字符串中排除) .这不允许在您的格式中嵌套 {...}