c++ is it safe to unget on fstreams
c++, fstream, iostream
Solution
`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.
http://ideone.com/JkDrwG
It's usually better to use your own buffer. Putback is a hack.
Problem
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?