Jump to content
  • Advertisement
Sign in to follow this  
ViperG

fstream

This topic is 4525 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

I have a problem and I cant get it to work. I'm doing this:
fstream file;
char szWord[80]={0}
file.open("test.txt", ios::in | ios::out);
file >> szWord;
file << szWord;
file.clear();
file.close();



The problem is my code wont write to the file after I have read in. I can write first then it works, but if I read before write it wont. No errors, it wont write to the text file. Shouldn't it read the first word from the text file then write the word right after the first word continues?

Share this post


Link to post
Share on other sites
Advertisement
No, that should be a no-op. There are separate get and put pointers, so the put pointer still points to the beginning of the file after the read operation, and the original content will get replaced with itself by the write.

BTW, reading into a char buffer with operator>> is exceedingly dangerous because it has no way of telling when the buffer is full. I wonder why it was included in the standard in the first place, particularly after all the grief caused by the infamous C library function gets().

Share this post


Link to post
Share on other sites
well then I suppose I can read until where I want then get my position in the buffer, then close the file and open it for writing and seek to that position then write as needed.

ops I see there are 2 pointers like you said cool ill test these out and see if I can match them.

/edit

well that didn\t work. resetting the get and put to 0 still wont let me write. ill just do it the old way and open and close the file.

[Edited by - ViperG on March 29, 2006 4:57:11 PM]

Share this post


Link to post
Share on other sites
No need to close in between. Just read as far as you need to, then do
file.pseek(file.gtell())
and merrily proceed writing.

Share this post


Link to post
Share on other sites
yeah I dunno if im doing something wrong weird stuff. cant get it to write if I read first. just wont write. hmmm

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!