Reputation: 2443
Suppose input.txt
is 1 byte text file:
std::ifstream fin("input.txt", std::ios::in);
fin.get(); // 1st byte extracted
fin.get(); // try to extract 2nd byte
std::cout << fin.eof(); // eof is triggered
fin.unget(); // return back
std::cout << fin.eof(); // eof is now reset
fin.get(); // try to extract 2nd byte, eof assumed
std::cout << fin.eof(); // no eof is triggered
Seems like unget()
breaks eof
flag triggering also it breaks file pointers. Am I doing something wrong?
Upvotes: 4
Views: 416
Reputation: 137930
eof
is not set, but neither is good
. The stream is ignoring operations because it's in a failure mode.
I cannot recall what unget
is supposed to do after EOF, but unget
goes right back into failure if I use clear
to allow a retry.
It's usually better to use your own buffer. Putback is a hack.
Upvotes: 3