i used sizeof() to retrieve the size of each fundamental type:
short int = 2 Byte
long int = 4 byte
char bm[2]=2 Byte
but when i use sizeof(bmfh) , it returns 16 !!!!
(as you can see the sum of all data types are 14)
???????
The compiler is just padding the structure to the nearest word boundary. It might decide to place members beginning at even byte-offsets, etc. for efficiency.
If you want to know what it's doing, clear all bits in its memory with memset, set all of the bits in each member, and then dump out the structure in binary or hex... Note that this should not be relied upon, it's compiler dependent.
This is a pretty whacky way to go about doing this. Do you know the order of the data that you are reading in the header already? If that's the case then you want to display the variable that you are assigning the data to not the size of the variable.
It looks like you have an alignment problem. The 2 bytes allocated for your array do not end on a 4 byte word boundary. So if your architecture uses 4 byte words the compiler may have added 2 bytes worth of padding after your array and before your four byte integer. That makes sure the 4 byte integer begins on a 4 byte word boundary.
You can possible instruct the compiler to pack more densely #pragma pack(2). But that is very non-portable. Also it won't solve any endian problems you may get reading binary integers in directly from a file like that.
I would be tempted to read in each field individually and make sure you have the right endianness when writing to the struct.
For a more complicated but more object oriented solution see these links. I have seen the pragma pack method used and it will work for many situations but it can also be confusing and error prone. There are also situations where you get a lot of confusing and weird compiler errors if you ever need to take the address of packed structure members (depending on the compiler). http://www.parashift.com/c++-faq-lite/serialization.html