Sign in to follow this  

Having some issues with fstream

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by KuroKitten
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.


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

Share this post


Link to post
Share on other sites
Quote:
Original post by load_bitmap_file
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?

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 this post


Link to post
Share on other sites
Quote:
Original post by KuroKitten
Hmm, well it seems to run smoother, however it's not printing anything ont he end of the file... the file remains unchanged =/


That is odd... code works fine on my machine and nprz's I think.

Quote:
Original post by nprz
Quote:
Original post by load_bitmap_file
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?

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.


Ah, that makes sense.

Share this post


Link to post
Share on other sites
are you using gcc-Mingw? because I often write things up in dev-cpp before implementing them in my main projects in visual studio, and today I found that this is happening to me as well. I had to open the file in an ofstream to output anything, the fstream object would simply not do it (even with ios::out and/or ios::app flags)

I havn't tried it in VC yet, but my guess is it's compiler specific, because i've never heard of this before until today.

OH I forgot:

You also need to flush or close it to save. If the program closes without flushing, nothing gets saved to disk, all operations were only conducted in memory. :X

[Edited by - AAAP on December 19, 2005 4:08:17 PM]

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this