eof question

Is there a specific code for "is eof" and "not eof"? Thanks.
closed account (ybf3AqkS)
Yes.

http://www.cplusplus.com/reference/ios/ios/eof/
Last edited on
I've already read that. It didn't help me understand the specific code for is eof and not eof.
closed account (ybf3AqkS)
Do you have any code? Your question is vague.
I do, but I am just trying to understand what this pseudocode means..

for example I have this "while(masterFile not EOF)"

and I also have "if(masterFile is EOF)"

I understand that (!(masterFile.eof())) is not eof, but I don't get the is eof pseudocode.
closed account (ybf3AqkS)
EOF means end of file.
Last edited on
Thanks, again I understand that. To better understand what i'm talking about here's the full program code, and on one of the loops my pseudocode is telling me to code "is eof". the rest of the pseudo says not eof.

So, while(!fin.eof()) basically translates to is not eof.
would (fin.eof()) translate to IS eof?

http://www.cplusplus.com/forum/general/200404/
closed account (ybf3AqkS)
would (fin.eof()) translate to IS eof? Yes
Okay, that's what I wasn't sure of. Thanks.
This is the wrong way to read a stream:
1
2
3
4
5
    while(!fin.eof())
    {
        fin.get(ch);
        cout << ch;
    }


fin.eof() does not check if the stream is at the end and does not predict the future; it only reports if a stream input operation ran past the end of file in the past. When you reach end of file, fin.eof() is false, fin.get(ch); runs past the end and so fails, leaving ch unmodified, and then cout << ch; prints that unmodified character from the last loop iteration (for a typical text file, the endline character), one more time. Only on the next iteration after that, fin.eof() is true.

The correct way to read a stream to end of file (or any other error) is
1
2
3
4
while(fin.get(ch))
{
  cout << ch;
}
Last edited on
Topic archived. No new replies allowed.