Structure error... A wierd one!
Hello!
I''m trying to make a .bmp file loader...
But the wierdest is that i get a strange error (data loss) when i''m trying to fill the structure with data.
struct BmpFileHeader
{
unsigned short type;
unsigned int size;
unsigned short reserved1;
unsigned short reserved2;
unsigned int offsetBytes;
};
The sizeof(BmpFileHeader); returns 16 while i want it to be 14... Why? Isn''t this struct 14 bytes in a windows 98 environment?
And when try to read from the file... it skips 2 bytes in the file. It starts to read byte 0 and 1 and then it skips byte 2 and 3. And after that it continues to fill in the struct correctly... Very wierd... Maybe someone has a simple solution?
ReadFile( file, // handle to file
buffer, // data buffer
14, // number of bytes to read
(unsigned long *) &count, // number of bytes read
NULL); // overlapped buffer
Btw... I''m using VC++ 6.0...
// Peter - Thanx!
Make sure that your compiler doesn''t add padding. Set the byte alignment of the compiler to 1. That should solve the problem. If you''re using MSVC, the default byte alignment is 8 bytes (16 can be divided by 8, 14 can''t). You have to set this to 1
-Philip
a better solution would be to use #pragma pack.
#pragma pack( push, 1 )
struct bla
{
...
}
#pragma pack( pop )
this sets alignment to 1 byte just for this struct.
#pragma pack( push, 1 )
struct bla
{
...
}
#pragma pack( pop )
this sets alignment to 1 byte just for this struct.
.entrox
As a general rule, you don''t read entire structures into memory from documented file formats exactly because of the different ways machines pad structures.
The proper way is to read in the data elements one at a time. If you really want your code to be strong, you create a big endian / little endian check at runtime and do the necessary conversions if required. This results in rock solid code that works.
The proper way is to read in the data elements one at a time. If you really want your code to be strong, you create a big endian / little endian check at runtime and do the necessary conversions if required. This results in rock solid code that works.
_______________________________
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.
How do you check for edianess?
Load a block of stuff into a 4 byte hole, then index it as a char?
hehe
and you don''t want to disable padding on the whole project!
Load a block of stuff into a 4 byte hole, then index it as a char?
hehe
and you don''t want to disable padding on the whole project!
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
quote: Original post by Magmai Kai Holmlor
How do you check for edianess?
Load a block of stuff into a 4 byte hole, then index it as a char?
hehe
and you don''t want to disable padding on the whole project!
Your question on endianess: Yes, basically.
Your last statement: You don''t change the padding. You initialize your structures by assigning the elements of your structure with the values you read in one at a time from the file. There is no disruption of the padded structures.
_______________________________
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement