You are not logged in.
This is driving me crazy...
I am writing a binary format for some data, which gets written in a two pass process. In the first pass, among other things, I write a pointer to where the data of the second pass will be written. At the time of the first pass this is a pointer to the end of the file.
Now comes the second pass. For the new data, if open the file in append mode I get different results than if I open it in write mode and then jump to the position where the new data should come. In the last case the previous data gets overwritten, however the new data should be identical in both cases. Guess what? It is not
I use several seekp operations in the second pass, and I think there lies the problem. Probably the reference point is changed when opening the file in append mode and in normal mode, but I can not find any documentation about it. Has anyone experience with this?
I also made a test where in the second pass I copy the contents of the file, byte by byte to a new file and start the second pass with the resulting ofstream. When doing this, it works correctly. But this is obviously not a practical solution.
Offline
Well, after trying different possibilities with the ios_base flags, the solution that seems to work is to open the file in read *and* write mode. Then the offsets seem to be again relative to the beginning of the file. I still find it strange and probably error prone, but at least at the moment I can continue.
Offline
Possibly whats going on is that in write only mode your not opening the old file to add new data into it your creating a new file so the file system may be allocating different sectors then the original file. In my admittedly unprofessional opinion you should be either opening the file for Reading and Writing.
When appending the reference would be to the insertion point (aka the end of the file from your first pass).
Check this site out for a better explanation..
http://stdcxx.apache.org/doc/stdlibug/30-3.html
Cheers,
Jon
Offline