Public Group

# 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.

## 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 on other sites
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 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 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 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

1. 1
2. 2
3. 3
4. 4
frob
14
5. 5

• 16
• 12
• 20
• 12
• 19
• ### Forum Statistics

• Total Topics
632168
• Total Posts
3004529

×