Skip to main content
GameDev.net gamedev.net
🔒 Locked

Write command eating my file?

Started by xria Aug 8, 2002 at 8:16 PM 4 replies 400+ views
Original Post
xria
xria
What would make the ofstream::write command shorten a file?? I am writing a char buffer to the middle of a binary archive file, and after all is done, I check the archive file and find that it''s exacly ((sizeof(char)* buffersize) + 1) smaller than it was before I wrote to it. I debuged it, but I don''t know what the heck would cause this in the first place, so I didn''t get very far. All I used was ofstream::write(buffer,ios::binary|ios::out), and it eats my file instead of writing over it. Even if you just write a bunch of crap or NULLS to the middle of a file the file shouldn''t be able to readjust itself to cover up the NULLS should it?
Oluseyi
Oluseyi
The second argument to write is the length of data to write, not the openprot. Also check that you are opening the file to append (then repositioning), not to overwrite (which will erase the entire file).
xria
xria
The write(Buffer,ios::...thing was just an idiotic typo because I was in a rush,sorry about that. What I''m trying to do is overwrite a certain section within the file. This section is my game''s save file, so I need to overwrite this save file within my archive. I call....

ofstream fout;
fout.open("ResourceFile.res",ios::binary | ios::out);
fout.seekp(SaveFileOffset,ios::beg);
fout.write(Savefilebuffer,SaveFilebuffersize);

and when I check the archive file''s size, the file is SaveFilebuffersize + 1 smaller than it was before I did this. The buffer is full of random chars at the moment, but that shouldn''t affect it being written to the correct spot. And it surely shouldn''t make it take a hunk out of my archive file.
There is nothing more complicated than this happening. And since my archive file it shortened, it can''t be read into my program correctly since some of the info at the end isn''t at it''s correct offset anymore.

Got me pretty confused. Thanks for the attemp though, oluseyi.

Fruny
Fruny
You still have to open the file for append (ios:app if you want to modify it. Just opening it for output (ios::out) erases the file first, then writes whatever it is you want to write..

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Oluseyi
Oluseyi
fout.open("ResourceFile.res",ios::binary | ios::out);

Replace with
fout.open("ResourceFile.res",ios::binary | ios::out | ios::app);
xria
xria
Ahhh, I see now. I misunderstood apend then, I read that it automatically writes new data to the end of the file, ignoring positioning. I appreciate the help guys, that was something I definetly needed cleared up.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.