I would like to know if and why seekg(0)
should not clear the eofbit
stream stream. I am at the point where I have already read the entire stream, so EOF
been achieved (but failbit
is not failbit
), and you want to return from seekg()
to the actual position and read some characters again. In this case, seekg(0)
seems to "work" with the eofbit
set, but as soon as I try to read from the stream, failbit is set. Is this logic correct or is my implementation bad? Should I recognize this case and manually clear the eofbit (if the error is not fixed)?
EDIT:
The following program provided by the reader gives different results in my implementation (mingw32-C ++. Exe (TDM-2 mingw32) 4.4.1):
#include <sstream> #include <iostream> #include <string> int main() { std::istringstream foo("AAA"); std::string a; foo >> a; std::cout << foo.eof() << " " << foo.fail() << std::endl; // 1 0 foo.seekg(0); std::cout << foo.eof() << " " << foo.fail() << std::endl; // 0 0 foo >> a; std::cout << foo.eof() << " " << foo.fail() << std::endl; // 1 0 foo >> a; std::cout << foo.eof() << " " << foo.fail() << std::endl; // 1 1 }
The comments above belong to the user who tried this program in its implementation. I get these results:
1 0 1 0 1 1 1 1
source share