I am trying to write to an existing blank spaced binary file of 1 MB in size via C++, at a particular position.
ofstream of( filename, ofstream::binary );
of.seekp(1600, of.beg);
of.write(arr, 1024);
of.close();
The blank file is created by writing this buffer to file before:
char *buffer = (char*)malloc(size);
memset(buffer, ' ', size);
But this code just truncates the entire blank space file of 1MB and writes this 1KB content and the size of the file becomes 1KB.
Tried using the app as well as ate mode too. When I tried app mode like this:
ofstream of( filename, ofstream::binary | ofstream::app );
of.seekp(1600, of.beg);
of.write(arr, 1024);
of.close();
It adds the content to the end of the file. Thus the file becomes of size 1MB+1KB.
What I am trying to do is overwrite a 1KB data(at present it is blank space) in the file with some data. So that it won't change the file size or any other data.
Where am I doing it wrong ?