Hello everyone,
I was in the process of replacing some C code with C++ counterparts. In particular I had a function which read from a .bmp file, the first task being to check the first 2 bytes of the file for
0x4D42, or
'BM'.
It works fine when I use the following C code, as I always have:
1 2 3
|
FILE* file = fopen("Some_Bitmap_File.bmp","rb");
unsigned short fileType;
fread(&fileType,2,1,file);
|
But I run into a problem when trying to do this:
1 2 3
|
ifstream infile("Some_Bitmap_File.bmp");
unsigned short fileType;
infile >> fileType;
|
The value of fileType comes out as
0xCCCC instead. I also tried using the
ios::binary flag to see if that had any effect, but it didn't. Are the characters being extracted in reverse order? If I extract one character at a time, I get the 'B' and the 'M' that I'm expecting, but the rest of the header info requires me to read 2 and 4-byte chunks at a time so I can check their individual values as I go along, so reading byte-by-byte won't suffice. My impression was that the stream would just automatically extract from itself the correct number of bytes needed to fill the size of the 'short' variable, without me specifying it like in the C function. Any help understanding this is
greatly appreciated.