edware <> writes:
> I want to read header data from a image file,
> and I have a struct that represent this header.
> But it seems like my compiler pads the data,
> resulting that my fread() call won't put the right
> things in the right place.
>
> The struct starts like this:
>
> struct header {
> char magic[2]; /* 2 bytes */
> uint32_t size; /* 4 bytes */
> ...
> };
>
> It seems like my compiler puts two extra
> bytes after the magic field, which will
> result in a wrong size value.
>
> Is there some standard way to disable the padding?
No. See question 2.12 in the comp.lang.c FAQ, <http://c-faq.com/>.
If the header in the file really contains a 2-byte magic number,
immediately followed by a 4-byte unsigned integer, the best approach
is probably to read it as an array of 6 bytes, then extract the bytes
you want, perhaps using memcpy(). You should also think about byte
ordering; is the size field stored high-order byte first or low-order
byte first?
--
Keith Thompson (The_Other_Keith)
kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.