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

时间:2016-11-01 21:33:17

标签: c++ ifstream formatted-input

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

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

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

1 个答案:

答案 0 :(得分:3)

您的方法和代码存在一些问题:

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

因此,我建议您使用以下框架,逐行读取文件,并使用stringstream分别解析每一行。请注意,我只使用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

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