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

标签 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

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

最佳答案

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

  • 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()阅读直到结束字符(后者被消耗但从字符串中排除)。这不允许嵌套 {...}在你的格式。

关于C++:从具有多个定界符的txt文件中读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40369008/

相关文章:

c++ - 如何读入文件的末尾(或刚好在末尾之前)的字符?

c++ - 如何终止 cin>> 输入特定的单词/字母/数字/等

c++ - (c++) 使用 ifstream 将 .dat 文件读取为十六进制

python-2.7 - 带有强制前导零的python科学记数法

c++ - 格式不匹配时停止阅读

c++ - 为什么 std::filesystem::path::compare() 和 boost::filesystem::path::compare() 不同?

c++ - 如何为二进制图像中的形状重新着色以进行索引(填充)?

c++ - 建议为 C++ 选择线性代数库

c++ - vector 的 vector 和自定义类之间的区别

c++ - 逐行读取txt