【问题标题】:C++ EOF Never Set During While LoopC++ EOF 在 While 循环期间从不设置
【发布时间】:2012-10-04 09:44:00
【问题描述】:

所以基本上,我正在编写一个程序来接收一组数据并将结果输入到结构向量中。但是,我遇到的问题是文件结束标志从未在位于主函数中的 while 循环中设置

int main()
{
ifstream machineRecords;
machineRecords.open ("records.txt");
if (machineRecords.fail())
{
    cout << "Failed to open \'records.txt\' please make sure it is in the same directory as the executable.";
    exit(1);
}

char tempChar;
string record;
do
{
    int LINE = 0;
    for (int x = 0; x <= 11; x++)
    {
        cout << "Begin storing characters in record" << endl;
        do  //Stores one character at a time into the node string until it discovers the end of the node
        {
            tempChar = machineRecords.get();
            record += tempChar;
            cout << "||" << tempChar << endl;
            //_getch();
        } while (tempChar != '|' && tempChar != '\n');
        eraseCharFromString(record, '|');       //Removes ending tag character
        eraseCharFromString(record, '\n');  //Removes any sneaky newline characters

        cout << "Record: " << record << endl;

        switch(x)
        {
            case 0:
                machines[LINE].idNumber = atoi(record.c_str());
                break;
            case 1:
                machines[LINE].description = record;
                break;
            case 2:
                machines[LINE].purchaseDate.month = atoi(record.c_str());
                break;
            case 3:
                machines[LINE].purchaseDate.day = atoi(record.c_str());
                break;
            case 4:
                machines[LINE].purchaseDate.year = atoi(record.c_str());
                break;
            case 5:
                machines[LINE].cost = atof(record.c_str());
                break;
            case 6:
                machines[LINE].history.failRate = atof(record.c_str());
                break;
            case 7:
                machines[LINE].history.downDays = atoi(record.c_str());
                break;
            case 8:
                machines[LINE].history.lastServiced.month = atoi(record.c_str());
                break;
            case 9:
                machines[LINE].history.lastServiced.day = atoi(record.c_str());
                break;
            case 10:
                machines[LINE].history.lastServiced.year = atoi(record.c_str());
                break;
        }
        record = "";
        _getch();
    }

    tempChar = machineRecords.get();
    if (machineRecords.fail())
    {
        cout << "DONE" << endl;
        break;
    }
    else
    {
        machineRecords.putback(tempChar);
    }

    ++LINE;
    _getch();
} while (!machineRecords.eof());    //While not at the end of the file

recordFromLastDate(1999);

machineRecords.close();
_getch();
return 0;
}

任何格式错误都是 SO 的问题。

但无论如何,即使我尝试在每行之后读取另一个字符,它也不会触发 eof 标志。我真的不知道为什么不。

不幸的是,由于期中考试有大量其他项目,我没有时间重写太多代码,但我真的很想知道我是否做错了什么。

【问题讨论】:

  • eof() 在最后一次读取操作因文件结束条件而失败时设置。
  • @Omnifarious,我只是用它来做一些逐步的输出,因为我还不太熟悉步骤和断点。
  • @Vaughn,是的,当我添加 tempChar = machineRecords.get(); if (machineRecords.fail()) { cout &lt;&lt; "DONE" &lt;&lt; endl; break; } else { machineRecords.putback(tempChar); } 时,我就是这么想的,但我想我没有做正确的事情。

标签: c++ file-io eof


【解决方案1】:

EOF 仅在您读取超出文件末尾的字符时设置,因此明智的做法是认为 EOF 将在 machineRecords.get() 之后设置,您永远不会测试其返回值,因此后续读取将设置流的失败位和 EOF将被忽略。只需使用 -1 测试 machineRecords.get() 的结果以在继续之前捕获 EOF 或错误

【讨论】:

    【解决方案2】:

    我认为你应该尝试类似,

    char ch;
    ifstream ifile("filename");
    
    ifile.get(ch);   /*Called as prime read. Use getline() or ifile>>line where line is a string depending on your requirement*/
    while(ifile)
    {
    do your work;//
    ......
    ......
    ......
    ifile.get(ch);
    }
    

    这几乎适用于所有类型的文件。它首先从流中获取数据,然后在while循环的开头检查流的状态。如果失败,它将立即执行@之后的语句987654323@ 循环,否则它将继续直到达到EOF,流进入失败状态。

    【讨论】:

      【解决方案3】:

      你应该使用

      } while (!machineRecords.good()); 
      

      作为你的最终条件。

      它涵盖了 eofbit、failbit 和 badbit,而不仅仅是 eofbit,因此如果之前读取失败而未达到 eof,则循环也会结束。

      您可以在这里查看:http://www.cplusplus.com/reference/iostream/ios/good/

      【讨论】:

        猜你喜欢
        • 2019-07-26
        • 1970-01-01
        • 2017-12-07
        • 2012-11-21
        • 1970-01-01
        • 2012-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多