I have the need of writing the data contained in a struct to a binary file.
Lets say given the struct of
struct data {
unsigned char sig;
int reserved;
}
I want to be able to simply output the contents of this struct to a file.
I have tried to use this
fstream out("C:\\somepath", ios::out | ios::app | ios::binary);
data writedata;
writedata.sig = 'BM';
writedata.reserved = NULL;
out.write((char*)&writedata, sizeof(writedata));
out.close();
I expected the output would be (in hex, using a 32 bit compiler(so ints are 4 bytes)):
42 4D 00 00 00 00
But this is not what is printed.
Can someone explain why this is not working and the steps necessary to fix it?
Thanks.
'BM'to sig which is unsigned char.