Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C structs & fread

Name: Anonymous 2007-11-23 2:52

I have a struct i want to read in from file. To be specific its a windows bmp. It works when i read in the values individually, but when I try and read the structure as a whole it gets the wrong values:


struct BITMAPFILEHEADER{
    unsigned short bfType;        // size: 2
    unsigned int bfSize;        // size: 4
    unsigned short bfReserved1;    // size: 2 
    unsigned short bfReserved2;    // size: 2 
    unsigned int bfOffBits;    //size: 4
};

// works
fread (&fileheader.bfType,2,1,imgFile);
fread (&fileheader.bfSize,4,1,imgFile);
fread (&fileheader.bfReserved1,2,1,imgFile);
fread (&fileheader.bfReserved2,2,1,imgFile);
fread (&fileheader.bfOffBits,4,1,imgFile);

// does not work right
fread (&fileheader,sizeof(BITMAPFILEHEADER),1,imgFile);

Name: Anonymous 2007-11-23 3:03

Learn Erlang binary syntax.

Name: Anonymous 2007-11-23 3:04

Your struct is not necessarily with size 2 and 4 entries.  For optimization purposes, depending on how the compiler feels like fucking with you, it could 4-byte align each field.  Dunno if it's the problem, but it's a possibility.  Faggot

Name: Anonymous 2007-11-23 3:12

Name: Anonymous 2007-11-23 3:14

>>3
I looked at other code on the internet, and they use the same method

Is there a way to make sure the struct gets set the right way? COCKSUCKER

Name: Anonymous 2007-11-23 3:18

>>4
how would i use that?

Name: Anonymous 2007-11-23 3:33

>>1
either turn off forced alingment or expand your struct with 2 unused bytes, dumbass

Name: Anonymous 2007-11-23 3:38

>>6
got it, thanks alot >>4 =D

Name: Anonymous 2007-11-23 5:25

ONE WORD, THE FORCED ALIGNMENT OF DATA

Name: Anonymous 2007-11-23 5:34

Also don't forget your byte swapping.

Name: Anonymous 2007-11-23 8:16

fread (&fileheader.bfType,2,1,imgFile);
fread (&fileheader.bfSize,4,1,imgFile);
fread (&fileheader.bfReserved1,2,1,imgFile);
fread (&fileheader.bfReserved2,2,1,imgFile);
fread (&fileheader.bfOffBits,4,1,imgFile);

Learn to use the sizeof operator you stupid fuck instead of doing some elitist faggotry and directly writing the size.

Name: Anonymous 2007-11-23 8:21

1. Do not depend on specific sizes of int, shorts etc. Use stdint.h for that.
2. Do not read shit into a struct. The only guarantee about that is that you will get the same back if you wrote the struct with the exact same binary. Read shit into an array of u8_t's and extract the values from that.
3. Do not depend on the endianness of the computer. Use a macro to get shit by shifting the u8_t's (you can ifdef the macro to use type punning if appropriate).
4. Never believe something on the Internet without doublechecking.

Name: Anonymous 2007-11-23 8:52

>>11
Uh, no. Element sizes in a file are determined by the format, not the target runtime.

Name: Anonymous 2007-11-23 15:39

>>13
Uh, uh, uh.

Name: Anonymous 2007-11-23 15:51

Uh considered harmful

Name: Anonymous 2007-11-23 19:49

considered harmful considered harmful

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List