• C

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
Thorn01Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
FrancozCommented:
Try the alternative

fread(&BMP_File, sizeof(_uint16) + 3*sizeof(_uint32),1, infile);
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.