fstream problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to read a text file, "rewind" it to the beginning and read it again.
I've tried using fstream's seekg(0, ios_base::beg) method with no luck. For
some reason it's still thinking it's at eof, even after the seekg is
executed. I've also tried closing the fstream and reopening it. Nothing is
working. Anyone have any ideas?

How do you read a file twice in VC++?
 
Michael said:
I need to read a text file, "rewind" it to the beginning and read it again.
I've tried using fstream's seekg(0, ios_base::beg) method with no luck. For
some reason it's still thinking it's at eof, even after the seekg is
executed. I've also tried closing the fstream and reopening it. Nothing is
working. Anyone have any ideas?

How do you read a file twice in VC++?

Michael:

You probably need to call clear().

David Wilkinson
 
Thanks David. You're right, that's what I did to fix it in fact, just took
me a while to figure it out. I'm surprised that if you seekg to the
beginning of the file you have to clear() to reset the eof and other flags.
Strange.

Thanks again.
 
Michael C said:
Thanks David. You're right, that's what I did to fix it in fact, just
took
me a while to figure it out. I'm surprised that if you seekg to the
beginning of the file you have to clear() to reset the eof and other
flags.
Strange.

That's the way standard I/O streams work : Once a stream is in error state
(and having the stream go past the end of the file put the stream into error
state, so that you can iterate through the file with a "while (mystream)"
loop), you cannot do anything on the stream : You must call clear before.

Arnaud
MVP - VC
 
Arnaud Debaene said:
That's the way standard I/O streams work : Once a stream is in error state
(and having the stream go past the end of the file put the stream into
error state, so that you can iterate through the file with a "while
(mystream)" loop), you cannot do anything on the stream : You must call
clear before.

Arnaud
MVP - VC

I haven't tested it yet, but after googling "seekg" I found plenty of
examples that seekg() to the end of file, then seekg() to other positions in
a file, without a clear() operation. That's what led me to believe it
wasn't necessary at first.

Thanks
 
Mike C# said:
I haven't tested it yet, but after googling "seekg" I found plenty of
examples that seekg() to the end of file, then seekg() to other positions
in a file, without a clear() operation. That's what led me to believe it
wasn't necessary at first.

Yeah, but by reading the file till the end, you've (probably) got the stream
go one *after* the end of the file, this is what put the stream in error.

Arnaud
MVP - VC
 
Arnaud Debaene said:
Yeah, but by reading the file till the end, you've (probably) got the
stream go one *after* the end of the file, this is what put the stream in
error.

Arnaud
MVP - VC

Ah, that makes sense. I'm using the getline() method to read a line at a
time. I didn't realize getline() read one past the eof - just assumed it
stopped once it encountered an eof.

Thanks again
 
Back
Top