I'm writing an application to patch assembler instructions according to memory addresses. I have been using C/stdio for FILE operations (fopen, fseek, fprintf), but I would like to have a C++/fstream implementation as well (.open, .seekp, .put). I've had no problems patching file memory with stdio, but fstream/ofstream methods corrupt my executable. I've tried opening the files in multiple ios modes, but nothing's worked. Here's a simple example.
Working C Implementation
#include <stdio.h>
int main() {
int offset[] = {0x0000082A, 0x0000082B, 0x0000082C, 0x0000082D};
char data[] = {0x90, 0x90, 0x90, 0x90};
FILE *faddr;
if (((faddr=fopen("test.exe", "r+")))==NULL) return -1
for (int i=0; i<sizeof(offset)/sizeof(int); i++) {
fseek(faddr, offset[i], SEEK_SET);
fprintf(faddr, "%c", data[i]);
}
return 0;
}
Not-Working C++ Implementation
#include <fstream>
int main() {
ofstream f("test.exe", ios::out | ios::binary);
if (f.fail()) return -1;
int offset[] = {0x0000082A, 0x0000082B, 0x0000082C, 0x0000082D};
char data[] = {0x90, 0x90, 0x90, 0x90};
for (int i=0; i<sizeof(offset)/sizeof(int); i++) {
f.seekp(offset[i], ios::beg);
f.put(data[i]);
}
f.close();
return 0;
}