0

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

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

4

3 回答 3

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循环后的语句,否则将继续执行直到EOFis到达流进入失败状态的地方。

于 2012-10-14T01:04:47.650 回答
1

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

于 2012-10-14T01:08:55.263 回答
-1

你应该使用

} while (!machineRecords.good()); 

作为你的最终条件。

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

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

于 2012-10-14T01:08:29.003 回答