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

x
  • 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
}BMP_File;

Using this method to read data in works fine but I don't want to do it this way.
------------------ Method 1
fread(&BMP_File.Type,sizeof(_uint16),1,infile);
fread(&BMP_File.Size,sizeof(_uint32),1,infile);
fread(&BMP_File.Reserverd,sizeof(_uint32),1,infile);
fread(&BMP_File.Offset,sizeof(_uint32),1,infile);

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
0
Thorn01
Asked:
Thorn01
1 Solution
 
ExceterCommented:
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.

Exceter
0
 
honey_hamsterCommented:
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).
0
 
KocilCommented:
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:
http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_20557282.html

So the effort will be the same.

0
 
FrancozCommented:
Try the alternative

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

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