eof question

Oct 21, 2016 at 5:21am
Is there a specific code for "is eof" and "not eof"? Thanks.
Oct 21, 2016 at 5:27am
closed account (ybf3AqkS)
Yes.

http://www.cplusplus.com/reference/ios/ios/eof/
Last edited on Oct 21, 2016 at 5:32am
Oct 21, 2016 at 5:40am
I've already read that. It didn't help me understand the specific code for is eof and not eof.
Oct 21, 2016 at 5:42am
closed account (ybf3AqkS)
Do you have any code? Your question is vague.
Oct 21, 2016 at 5:44am
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.
Oct 21, 2016 at 5:49am
closed account (ybf3AqkS)
EOF means end of file.
Last edited on Oct 21, 2016 at 11:29pm
Oct 21, 2016 at 5:54am
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/
Oct 21, 2016 at 5:56am
closed account (ybf3AqkS)
would (fin.eof()) translate to IS eof? Yes
Oct 21, 2016 at 6:02am
Okay, that's what I wasn't sure of. Thanks.
Oct 21, 2016 at 12:46pm
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 Oct 21, 2016 at 3:08pm
Topic archived. No new replies allowed.