Is it possible to update only a part of a file in c++ ?
Example:
Old File A: 'A''A''A''B''B''C''C''C'
New File A: 'A''A''A''X''X''C''C''C'
as the real files are not as tiny like these examples, and I do know exactly what has changed ( offset and writeLenght for changed content ) it would be great to be able to open a file, set the stream to the right position, write the information and close the file again.... but this will lead to a file that looks like this:
Updated File: '0''0''0''X''X''C''C''C'
This is the code I used:
void update file( list<unsigned char> content, int offset){
fs::basic_ofstream< char > fileStream( path , ios::out | ios::binary );
list< unsigned char >::const_iterator contentIter = content.begin();
// begin write operation at the offset
advance( contentIter , offset);
fileStream.seekp( offset );
while( contentIter != content.end() ){
unsigned char value = (char)*contentIter;
fileStream.put( value );
++contentIter;
}
fileStream.close();
Is there a way to do this, or has the whole file to be rewritten everytime it changes ?
Thank you