c++ - file stream tellg/tellp and gcc-4.6 is this a bug? -
this code:
#include <iostream> #include <cstdio> #include <fstream> #include <string> int main() { std::remove("test.txt"); std::fstream f("test.txt",std::ios::in | std::ios::out | std::ios::binary | std::ios::trunc); std::cout << f.good() << std::endl; f<<"test"<< std::flush; std::cout << f.tellg() << " " << f.tellp() << std::endl; f.seekg(0); std::string s; f>>s; std::cout << f.tellg() << " " << f.tellp() << std::endl; }
gives following output in gcc-4.4.5
1 4 4 4 4
i.e. both tellg , tellp returned expected stream position 4.
while gcc-4.6.0
gives:
1 4 4 -1 4
where can find reference tell:
- 1st case correct (bug in gcc-4.6)
- 2nd case correct (bug in gcc < gcc-4.6)
- both case correct behavior undefined
ok, not bug, seems required behavior:
according c++ 2003 standard:
tellg(): (27.6.1.3)
after constructing sentry object, if fail() != false, returns pos_type(-1) indicate failure. otherwise, returns rdbuf()->pubseekoff(0, cur, in).
sentry (27.6.1.1.2):
if noskipws 0 , is.flags() & ios_base::skipws nonzero, func- tion extracts , discards each character long next available input character c whitespace character. if is.rdbuf()->sbumpc() or is.rdbuf()->sgetc() returns traits::eof(), function calls setstate(failbit | eofbit) (which may throw ios_base::failure).
so
- tellg() creates sentry object:
- sentry extracts white space characters , should set failbit after getting eof.
- tellg() sees failbit should return eof() (-1)
so gcc-4.6 seems behave correctly...
Comments
Post a Comment