GCC 4.7 istream::tellg() returns -1 after reaching EOF

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 17:24:39

According to C++11 section 27.7.2.3p40,

if fail() != false, returns pos_type(-1)

So gcc 4.7 has the correct behavior for the current version of C++ (assuming that peek() at end of stream causes failbit to be set, and it does during sentry construction, since skipws is set by default).

Looking at the wording of C++03, it is the same. 27.6.1.3p37. So the behavior you describe in gcc 4.4 is a bug.

To be precise, eofbit won't cause tellg() to return -1. But the fact that you read past EOF sets the failbit, and tellg() will return -1 if badbit or failbit are set.

The solution is to clear the status flags before calling tellg():

iss.clear();
iss.tellg();  // should work
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!