I can't seem to get my code to save the buffer into the same binary file. I read the file and placed it into a buffer, and after changing the values of the buffer at a position, I want to overwrite it and place it back into the file.
#include <iostream>
#include <fstream>
usingnamespace std ;
int main(){
// Open file as binary
fstream file;
file.open( "binaryFile", ios::in|ios::binary);
if(file.is_open()){
// Local variables
int size = 0;
char* buffer;
// Get the length of the file
file.seekg(0, file.end);
size = file.tellg() ;
// Go back to the top of the file
file.seekg(0, file.beg);
// Create a buffer and read file into buffer
buffer = newchar[size];
file.read( buffer, size );
// Change value of the hex
for ( int i = 0; i < size; i++ ){
if( buffer[i] == 127){
buffer[i] = 126;
printf("%X ", buffer[i]);
}
}
// Write it back to the file
file.seekg(0, file.beg);
for ( int i = 0; i < size; i++ ){
file << buffer[i];
}
delete [] buffer;
file.close();
}
else{
cout << "Unable to open file" << endl;
}
return 0;
}