C ++ EOF永远不会在循环期间设置

时间:2012-10-14 00:38:10

标签: c++ file-io eof

基本上,我正在编写一个程序来接收一组数据并将结果输入到结构向量中。但是,我遇到的问题是文件结束标志永远不会在主函数中的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标志。我真的不知道为什么不。

不幸的是我没有时间重写代码,因为有大量的其他项目用于midterms,但我真的想知道我是做了什么完全错误或什么。

3 个答案:

答案 0 :(得分:1)

我认为你应该尝试类似的事情,

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循环开始时检查流的状态。如果失败,它将立即执行语句while循环,否则它将一直持续到EOF,直到流进入失败状态。

答案 1 :(得分:1)

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

答案 2 :(得分:-1)

你应该使用

} while (!machineRecords.good()); 

作为你的最后条件。

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

您可以在此处查找:http://www.cplusplus.com/reference/iostream/ios/good/