所以基本上,我正在编写一个程序来接收一组数据并将结果输入到结构向量中。但是,我遇到的问题是文件结束标志从未在位于主函数中的 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 标志。我真的不知道为什么不。
不幸的是,我没有时间重写太多代码,因为有很多其他的期中项目,但我真的很想知道我是否做错了什么。