Why doesn't seekg (0) clear the state of the thread?

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 
+6
source share
2 answers

In accordance with the new standard clear() , reset eofbit (ยง 27.7.2.3):

basic_istream<charT,traits>& seekg(pos_type pos);

Effects: behaves like an unformatted input function ..., except that the function first clears eofbit ...

But in the old standard (ยง 27.6.1.3) there is no mention of eofbit cleaning!

And a simple test:

 #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 } 
+2
source

Why not just flush manually () the stream and then return after eofbit is installed? EOF has been achieved, why do I need to search for it automatically? This may cause more problems.

0
source

Source: https://habr.com/ru/post/913130/


All Articles