So basically, I’m writing a program to take in a set of data and input the results into a vector of structures. However, the issue I am having is with the end of file flag never being set in my while loop located in the main function
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;
}
Any formatting errors are SO’s problem.
But anyway, even when I try to test read another character after each line, it simply will not trigger the eof flag. I really have no clue why not.
Unfortunately I don’t have the time to rewrite too much the code because of a ton of other projects for midterms, but I really wanted to know if I was doing something completely wrong or what.
EOF only set when you read a character beyond end of file, so it is wise to think EOF will be set after
machineRecords.get()that you never test its return value, so subsequent reads will set failure bit of the stream and EOF will be ignored. just test result ofmachineRecords.get()with -1 to catch EOF or error before continue