Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

Load data from a file to a struct

Im Trying to read the following file to a structure

424D E8040000 00000000 36000000
|--| |------| |------| |------|
Type Size     Reserved Offset

_uint16 = unsigned short int
_uint32 = unsigned long

struct BHEADER
_uint16 Type; //BM normally
_uint32 Size; //Length of file
_uint32 Reserverd; //Unused
_uint32 Offset; //Information Offset in file

Using this method to read data in works fine but I don't want to do it this way.
------------------ Method 1

Way I would like to do it is like this in a single line.
-------------------- Method 2
fread(&BMP_File, sizeof(BMP_File),1, infile);

Mem Dumps of Structure

424D E8040000 00000000 36000000 <-- File
|--| |------| |------| |------|
424D 0000E804 00000000 00003600 <-- Method 1 - Works
|--| |------| |------| |------|
424D E8040000 00000000 36000000 <-- Method 2

Anybody know how to load this struct without doing each member individually if possible?

Thanks in advance
1 Solution
I could be wrong here but it appears that you are having endian difficulties. You will notice that what method number two reads in is exactly the same as what is written on the disk.

Exeter is right about endian-issues.  Is your file little-endian, or big-endian?  In other words, is the file-size in your example 0x04E8 (1256 bytes), or 0xE8040000 (3892576256 bytes).  It appears to me like method2 is more likely to be working than method1!  To be honest, I don't know how you got the results you did with method1.  Maybe you can printf() each of the fields of BMP_File - it'll be easier for you to debug than your memory dumps.

Having said that, method2 is fine as long as you always use the same compiler and the same target CPU.  If you change either, then there's a chance that the compiler will pad your BHEADER struct.  And if you change CPU, there's a chance that the endian-ness of your fields will get messed up (although in that case, method 1 won't work either).
This is 16 bit (2 byte) endian problem.

424D E8040000 00000000 36000000 <-- File
|--| |------| |------| |------|
424D 0000E804 00000000 00003600 <-- Method 1 - Works
|--| |------| |------| |------|
424D E8040000 00000000 36000000 <-- Method 2

I guess the BMP-file was created on Windows, and you are trying to read it on Mac or Solaris.
Nothing you can do mate. Just do the Method 1.
If you read it with method2, you have to swap each uint32 data anyway. See:

So the effort will be the same.

Try the alternative

fread(&BMP_File, sizeof(_uint16) + 3*sizeof(_uint32),1, infile);

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now