kenney@milton.u.washington.edu (Michael Kenney) (05/24/91)
I've been trying to get the hang of C++ streams and I've run into an anoying problem. I'm trying to read a stream to EOF, rewind to the beginning and read it again, ie: func(istream &in) { . . . while(in.get(c)) { // Do something } in.seekg(0, ios::beg); // This loop FAILS while(in.get(c)) { // Do something else } } The problem is, the second while loop never iterates, in.get() returns 0. I used in.tellg() to insure that the stream is repositioned, and it is. Am I missing something obvious? Please email your responses. Thanks. Mike Kenney UW Applied Physics Lab mikek@apl.washington.edu mike@apl-at.apl.washington.edu
kenney@milton.u.washington.edu (Michael Kenney) (05/24/91)
In article <1991May23.181402.7912@milton.u.washington.edu> kenney@milton.u.washington.edu (Michael Kenney) writes: >I've been trying to get the hang of C++ streams and I've run into an >anoying problem. I'm trying to read a stream to EOF, rewind to the >beginning and read it again, ie: > [ example deleted ] >The problem is, the second while loop never iterates, in.get() returns >0. I used in.tellg() to insure that the stream is repositioned, and it >is. Am I missing something obvious? > >Please email your responses. > >Thanks. > >Mike Kenney >UW Applied Physics Lab >mikek@apl.washington.edu >mike@apl-at.apl.washington.edu > It was pointed out to me that seekg() does not reset the "eofbit", I need to call clear(). Thanks to all who replied. Mike Kenney UW Applied Physics Lab mikek@apl.washington.edu mike@apl-at.apl.washington.edu
rae@alias.com (Reid Ellis) (05/26/91)
Michael Kenney <kenney@milton.u.washington.edu> writes: | func(istream &in) | { | . | . | . | while(in.get(c)) | { | // Do something | } | | in.seekg(0, ios::beg); | | // This loop FAILS | while(in.get(c)) | { | // Do something else | } | } | |The problem is, the second while loop never iterates, in.get() returns |0. I used in.tellg() to insure that the stream is repositioned, and it |is. Am I missing something obvious? Well, you *are* missing something, but no, it isn't obvious [which is why I'm posting]. I discovered this when I derived a couple of classes from fstream and streambuf. The problem is that ios [from which both istream and ostream are derived] maintains a state within itself, WHICH IS NOT RESET WHEN YOU 'REWIND' THE STREAM. It took me a few hours to figure this one out. So, all you have to do is reset the error state of the stream, by saying "in.clear();" after you hit EOF. I don't think it matters if you call clear() before or after the seekg(). Hope this helps other people bitten by the same thing. I'm still of two minds about whether istream::seekg() [and ostream::seekp() for that matter] should reset the ios::eofbit bit of the error state.. Reid -- Reid Ellis rae@utcs.toronto.edu || rae@alias.com CDA0610@applelink.apple.com || +1 416 362 9181 [work]