Public Group

# Having some issues with fstream

This topic is 4683 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

For some reason, once the stream gets to the EOF, it stops working properly. I can't move anywhere else in the stream, I can't write naything to it, or read anything from it. Anything I attempt to read is spimply blank, anything I try to write never gets written, and when ever I try to move back to the begining it doesn't budge =/ here's a little test program:
/*The program*/
#include <fstream>
#include <string>

int main ()
{
string s;
string t;
fstream File("myfile.txt", ios::in | ios::out);

while(File >> s) cout << s << endl;
File.seekp(ios::beg);
File >> t;
File.seekp(ios::end);
File << t;

}

myfile.txt ------------------
abc 123 look at me!

the output is:
abc
123
look
at
me!
<return>

and myfile.txt ends up looking like this: ------------------
abc 123 look at me!

=/ if anyone has any ideas, big thanks^^ Meow.

##### Share on other sites
I think you want seekg and not seekp.

EDIT:
Quote:
 Original post by KuroKittenMeow.

[attention]

##### Share on other sites
When your stream hits the EOF, try using clear() to reset the status bits on the stream.

##### Share on other sites
well, i tried using seekg() and that didn't end up any different. i'm not sure where i should use clear()... i tried it in some places, and again, no difference...

lol, yeah, i always try to end posts with a meow of some sort, adds flavor XD

Mrow!

##### Share on other sites
Okay, I tested it out myself, so this should work:

/*The program*/#include <fstream>#include <string>int main (){	string s;	string t;	fstream File("myfile.txt", ios::in | ios::out);		while(File >> s) cout << s << endl;	File.clear();	File.seekg(0, ios::beg);	File >> t;	File.seekp(0, ios::end);	File << t;}

I think what this means is that once you reach EOF for a stream it gets into a bad state so you have to clear it first, then seek back to the beginning.

EDIT2: Okay, fixed. I don't know why File.seekp(0, ios::end) works but File.seekp(ios::end) doesn't. Anyone know why?

EDIT3: According to a nice book I have once you reach EOF an EOF bit is set for the status flag, so that's why the file needs to be cleared before doing anything else, since it's considered an error flag (the stream isn't considered good anymore). Also, the ios::beg/ios::end/ios::cur flags are meant for relative positions, which is why it has to be File.seekp(0, ios::end) instead of File.seekp(ios::end). Also, that means it should be File.seekg(0, ios::beg) too. I don't know why File.seekg(ios::beg) seemed to work though. Anyone know why?

[Edited by - load_bitmap_file on December 18, 2005 10:16:14 PM]

##### Share on other sites
I'm still getting weird stuff with that... It seems to move around better now, but it still wont right to the file... if i open the file with an additional ios::app command it appends fine, but always returns down before doing so, and doesn't seem to read fromt he file right =/

Mreow.

##### Share on other sites
Quote:
 Original post by KuroKittenI'm still getting weird stuff with that... It seems to move around better now, but it still wont right to the file... if i open the file with an additional ios::app command it appends fine, but always returns down before doing so, and doesn't seem to read fromt he file right =/Mreow.

Sorry, I posted prematurely [embarrass]. My previous post has the program correct now.

##### Share on other sites
Hmm, well it seems to run smoother, however it's not printing anything ont he end of the file... the file remains unchanged =/

##### Share on other sites
Quote:
 Original post by load_bitmap_fileAlso, that means it should be File.seekg(0, ios::beg) too. I don't know why File.seekg(ios::beg) seemed to work though. Anyone know why?

My guess would be that ios::beg is enumerated as 0, ios::cur is 1, and ios::end is 2. So if you pass ios::beg in as a position, it would behave ok. ios::end or ios::cur will not. Another reason C++ is evil [evil].

-edit- But your code that you have now works for me.

[Edited by - nprz on December 18, 2005 11:09:19 PM]

##### Share on other sites
pass in ios::end-1 and that should put the ptr right before the eof

1. 1
2. 2
3. 3
4. 4
Rutin
15
5. 5

• 14
• 9
• 10
• 12
• 17
• ### Forum Statistics

• Total Topics
632910
• Total Posts
3009176
• ### Who's Online (See full list)

There are no registered users currently online

×