eof() doens't look at the input buffer for it's material like other istream methods such as >> or get() or getline() do. It looks at the state variable of objects derived from the ios class, like ifstream objects. Embedded within the state variable is a bit that denotes whether EOF has been encountered or not. If it has then eof() returns non-zero, which means true, and if it hasn't then eof() returns zero, which means false.
So what? Well, this information is mission critical when reading from files and trying to terminate appropriately when reaching the end of file, as evidenced by finding EOF. Lets say the file contents are:
abcdefg
and you are using this code to read the file:
Code:
char buff2=' ';
bool end=false;
// testing for if it exists here, not putting it in....
while(!script.eof() && !end)
// Test if at the end of the file or END
{
script.get(buff2);
//Get the current character
if(buff2==-1)
// Double check if we are at the end of the file
break;
//do something with buff2 here
}
first of all the value of end is never changed in the code once it is
initialized. It could have been changed after the if(buff2 == -1) statement, but break is used instead, so I would delete any reference to end.
second, I don't believe -1 is a valid char. It is a valid int, but I don't think it is a valid char. therefore the if(buff2 == -1) statement probably won't work in any event. You could check for some valid char unlikely to be in your file, say a tilde (~) and then the syntax would work. Or you can just drop this completely and use the above knowledge of EOF and eof() to do the job for you.
note in the above code that no script methods/operators are called before the first call to eof(). Therefore the state variable of script should have the eofbit indicating that EOF has not been found when eof() is called and checks it the first time. Therefore the body of the loop proceeds fine. get() is used to get the first char in the input stream (file). The char isn't EOF so the eofbit in the state variable isn't toggled. And you repeat this 5 times until the f has been read. EOF isn't found at the read of f, so eof() still returns false when it checks the state variable and the next call to get() finds g. g isn't EOF, so the eofbit still isn't toggled, so eof() still will return true even though we have read the last item in the file. OOPS!. Now what happens. Well the file conditional is still true so the loop has to be done one more time, but now you are in no mans land because EOF isn't a storeable char so the program does what it wants (often it will use the last accepted data and repeat it, but don't count on that happening).
So what to do. Well call an istream method just before the call to eof(). Like this:
Code:
char buff2=' ';
bool end=false;
//first call to script method
script.get(buff2);
//if file is empty then get EOF found and eofbit is toggled
//if file isn't empty then EOF isn't found and eofbit isn't toggled
while(!script.eof())
{
//do something with buff2 here
//now try it again
script.get(buff2);
//if EOF found, eofbit is toggled and eof() will return true so loop will stop without the one extra loop
//if EOF not found, eofbit won't be toggled and eof() will return false so loop will continue at least one more time
}
And there you have it. Slimmed down, buffed up, and ready to go, with a tidy explanation that may dazzle your friends and acquantainces.
Good luck.