TOPIC 11.3.2
Differences in Locating the End of a
File
The Macintosh CodeWarrior compiler does not support the eof function used in this chapter. Consult CodeWarrior's documentation if you are using the CodeWarrior compiler.
The Macintosh Symantec C++ compiler reports the end of the file before the attempt to read past the last element. Therefore, some programs in the exercises may fail to print the last element of the file. To fix this behavior, remove the if statement around the output statements.
Due to differences in operating systems, the eof function may not work properly on some computers. In this case the fail function will work just as well, if not better.
The fail function returns true if anything goes wrong with the file access. The syntax is basicly the same as that of the eof function. An examples follows:
ifstream infile; // file variable
int index; // index
int num[20]; // array of ints
infile.open("myFile.txt", ios::in); // open file
while(!infile.fail() && index < 20)
{
if(!infile.fail()) // notice use of
{ // infile.fail()
infile << num[index];
index++;
}
}