ofstream writing replaces entire file with 0s
I have a std::vector of short ints that I need to write to a specific location in a binary file (without using .NET code). To that end, I wrote up this code:
Code:
ofstream fileStream (filePathString, ios::out | ios::binary);
int curPos = 2821;
fileStream.seekp(curPos);
int iter = 0;
while (iter < 1024*1024){
char bytesToWrite[2];
//Low byte
bytesToWrite[0] = LOBYTE(dataVector[iter]);
//High byte
bytesToWrite[1] = HIBYTE(dataVector[iter]);
fileStream.write(&bytesToWrite[0], 2);
curPos += 2;
fileStream.seekp(curPos);
iter++;
delete[] bytesToWrite;
}
fileStream.close();
The code runs without crashing, but when I look at the file afterwards in a hex editor, every byte (even those outside the range I thought I was writing to) are replaced with 00. I suspect I'm missing something in my understanding of file streams. Did I write that code correctly? Seekp does move the pointer over the next byte to be overwritten, yes? Am I getting a memory leak somewhere?
Re: ofstream writing replaces entire file with 0s
1) you should not "delete [] bytesToWrite" ... byteToWrite was not created using new []
2) You have:
Code:
ofstream fileStream (filePathString, ios::out | ios::binary);
int curPos = 2821;
fileStream.seekp(curPos);
I do not know what the seekp will do, since you create an empty file
and try to seek to position 2821 (which does not exist)
3) I assume that the file already exists, so you can try openning with:
Code:
ofstream fileStream (filePathString, ios::out | ios::in | ios::binary);
4) I think the entire loop could be replace with:
Code:
fileStream.write(reinterpret_cast<const char *>(&dataVector[0]),1024*1024*sizeof(short));
Re: ofstream writing replaces entire file with 0s
Huh. That single line replacing the entire loop worked perfectly. Thanks! :)
But what do you mean I was creating an empty file? You're right, filePathString is the path to a file that already exists. I thought this:
Code:
ofstream fileStream (filePathString, ios::out | ios::binary);
would open that file, and then seekp would work as expected? What's the difference between what I have and what you suggested in #3?
Re: ofstream writing replaces entire file with 0s
Quote:
Originally Posted by
fiodis
The code runs without crashing,
Code:
char bytesToWrite[2];
//...
delete[] bytesToWrite;
This does not "run fine". You are calling delete[] on a pointer that was never initialized with new[]. Once that illegal line is executed, the behaviour of the program becomes undefined.
Regards,
Paul McKenzie
Re: ofstream writing replaces entire file with 0s
ios::out erases an existing file. So if you open an existing file with that option, then seekp to a position, all bytes before that position are filled with 0x0.
It's not Philip Nicoletti's suggestion #4 that did the trick, it's #3 which opens an existing file for read/write access. IIRC, using this option on a non-existing file, then trying to write to it would fail. Watch out for the pitfalls of the filestream::open options...
HTH,
Richard
Re: ofstream writing replaces entire file with 0s
I see. Thanks everyone! :)