?
Solved

Load data from a file to a struct

Posted on 2003-03-31
4
Medium Priority
?
249 Views
Last Modified: 2010-05-18
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
Comment
Question by:Thorn01
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 8

Expert Comment

by:Exceter
ID: 8242978
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
 
LVL 2

Expert Comment

by:honey_hamster
ID: 8243125
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
 
LVL 5

Accepted Solution

by:
Kocil earned 240 total points
ID: 8243332
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
 
LVL 2

Expert Comment

by:Francoz
ID: 8243431
Try the alternative

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

Featured Post

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question