I try to detect EOF of an std::istream
by advancing the input pointer with 1 step by calling
seekg( 1, std::ios_base::cur )
However seekg()
moves 1 position beyond EOF, when it sets the failbit
of the stream. The eofbit
is never set. See the output of this test program:
#include <iostream>
#include <sstream>
using namespace std;
void info( int aRelativePos, istream& aStream )
{
cout << "POS=" << aRelativePos <<
" input pos=" << aStream.tellg() <<
" char=" << aStream.peek() <<
"\tGood: " << aStream.good() <<
" Eof: " << aStream.eof() <<
" Bad: " << aStream.bad() <<
" Fail: " << aStream.fail() << "\n";
}
int main()
{
istringstream input ("12");
int i=0;
while ( input.good() )
{
info( i, input );
input.seekg( 1, std::ios_base::cur ); //advance 1 step forward
++i;
}
info ( i, input );
return 0;
}
Output:
POS=0 input pos=0 char=49 Good: 1 Eof: 0 Bad: 0 Fail: 0
POS=1 input pos=1 char=50 Good: 1 Eof: 0 Bad: 0 Fail: 0
POS=2 input pos=-1 char=-1 Good: 1 Eof: 0 Bad: 0 Fail: 0
POS=3 input pos=-1 char=-1 Good: 0 Eof: 0 Bad: 0 Fail: 1
(Compiled by gcc 5.2 with -std=c++11
. You can run this code here: http://ift.tt/1OwXXcg )
Moreover MS document on seekg
( http://ift.tt/1S3qRaM ) says that relative positioning in text files is not supported by the C++ Standard.
But I could not find such info in the Standard... Can you please give me the reference?
Aucun commentaire:
Enregistrer un commentaire