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:
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?
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
Last edited by Paul McKenzie; January 3rd, 2013 at 05:04 PM.
ios:ut 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:pen options...
HTH,
Richard
Last edited by Richard.J; January 3rd, 2013 at 05:22 PM.
Reason: sorry, could not find the option to disable those smilies
Bookmarks