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

后端 未结 2 1086
小蘑菇
小蘑菇 2021-01-18 08:22

The following code works with gcc 4.4.
But gcc 4.7 will give assertion failure.

#include 
#include 
#include 

        
2条回答
  •  青春惊慌失措
    2021-01-18 08:49

    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
    

提交回复
热议问题