Hi,
I'm having trouble understanding a problem I came across when trying to read a bitmap header into a structure. I thought it was possible to read the header straight into a structure using the 'sizeof' function on the structure:
Code:
#include <iostream.h>
#include <fstream.h>
struct HEADER {
unsigned short bftype;
unsigned int bfsize;
unsigned short res1;
unsigned short res2;
unsigned int offset;
};
int main(void)
{
ifstream tester ("abitmap.bmp",ios::in|ios::binary);
HEADER bmfh;
tester.read((char *)&bmfh,sizeof(bmfh));
cout << "Bitmap file header contents" << "\n"
<<"bftype = " << bmfh.bftype << "\n"
<<"bfSIZE = " << bmfh.bfsize << "\n"
<<"bfReserved1 = " << bmfh.res1 << "\n"
<<"bfReserved2 = " << bmfh.res2 << "\n"
<<"bfOffbits = " << bmfh.offset << "\n \n \n";
return 1;
}
However, with this code the header was is not read in properly. I remember reading somewhere that elements in structures such as 'shorts' may fill more space in the structure than neccessary and believe this may be what is causing the problem. Is there any 'nice' way to solve this problem using a similar method to:
tester.read((char *)&bmfh,sizeof(bmfh));
I managed to read the header in using individual reads on each element is not very 'nice':
Code:
#include <iostream.h>
#include <fstream.h>
struct HEADER {
unsigned short bftype;
unsigned int bfsize;
unsigned short res1;
unsigned short res2;
unsigned int offset;
};
int main(void)
{
ifstream tester ("abitmap.bmp",ios::in|ios::binary);
if(!tester){
cout<<"Cannot read from file";
return 0;
}
unsigned short bftype;
unsigned int bfsize;
unsigned short res1;
unsigned short res2;
unsigned int offset;
tester.read((char*) &bftype,sizeof(bftype));
tester.read((char*) &bfsize,sizeof(bfsize));
tester.read((char*) &res1,sizeof(res1));
tester.read((char*) &res2,sizeof(res2));
tester.read((char*) &offset,sizeof(offset));
cout << "Bitmap file header contents" << "\n"
<<"bftype = " << bftype << "\n"
<<"bfSIZE = " << bfsize << "\n"
<<"bfReserved1 = " << res1 << "\n"
<<"bfReserved2 = " << res2 << "\n"
<<"bfOffbits = " << offset << "\n \n \n";
return 1;
}
thanks for any help,
brif