I am using fstream to write an integer to binary file.
int main(){
fstream f1;
int num = 2, num2 = 0;
f1.open("dat1", ios::app | ios::out | ios::in | ios::binary);
f1.write((char *)num, sizeof(int));
f1.seekp(0);
f1.read((char *)num2, sizeof(int));
cout << num2;
}
The problem is on line f1.write. I can write to binary file an array, but when I try to write just one block of int it gives me an error:
Unhandled exception at 0x522C7EA6 (msvcp120d.dll) in Project.exe: 0xC0000005: Access violation reading location 0x00000002.
I don't understand what the problem is.