从文本文件中读取多个分隔符



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

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],您从stringstream中提取单个字符,这将覆盖现有信息。
  • 当然还有其他问题。
因此,我建议您使用下面的框架,它逐行读取文件,并使用stringstream分别解析每行。请注意,我只使用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()读取直到结束字符(后者被使用,但从字符串中排除)。这不允许在格式中嵌套{...}

最新更新