
September 13th, 2004, 04:35 AM
|
|
Registered User
|
|
Join Date: Sep 2004
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Problem writing to a binary file
Hi all,
I am trying to output a struct to a binary file :
Code:
typedef struct {
unsigned char bChar;
unsigned char mChar;
unsigned int fileSize;
unsigned short int reserved1;
unsigned short int reserved2;
unsigned int offset;
} IMG_HEADER;
// I'v omitted setting struct vars..
ofstream oFile( fileName, ios::binary | ios::trunc | ios::out );
oFile.write( (char*) &imgHead, sizeof(IMG_HEADER) );
oFile.close();
When I view it with a hex editor, the first 2 chars are 'B' and 'M'
(correct) but then it outputs 'C' 'C' before the filesize. All the rest are output correctly.
If I change it to have just one unsigned char at the start it outputs 3 'C''s.. (and 0 'C''s
for no chars, 1 for 3 chars). It seems to be outputing the first int on a address of 4
or something and leaving the bytes after the first chars at their first setting?
Any ideas would be welcome.
|