Reputation: 625
In this operation I copy my source binary file first.
Then I wish to start overwriting bytes in the copied file starting from a specific offset.
I move with seekp(offset,std::ios::beg)
to the desired position and I start the overwriting process with
file.write(reinterpret_cast<const char*>(&my_vector[0]), my_vector.size()*sizeof(unsigned char));
Then close the file.
When I open processed file in hex editor all the bytes I see before the offset I have started writing are zeros and the bytes I have written with this operation are sucesfully written.
The mode of the stream is std::fstream(path, std::ios::out | std::ios::binary);
Is there something I am missing in this operation?
Upvotes: 1
Views: 1164
Reputation: 2819
If you want to preserve the old contents, open it in in/out mode. i.e. ios::in | ios::out
.
Additionally, if you're using std::fstream
this is the default behavior, so you could have just used: std::fstream(path, std::ios::binary)
.
Upvotes: 4