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
};
// does not work right
fread (&fileheader,sizeof(BITMAPFILEHEADER),1,imgFile);
Name:
Anonymous2007-11-23 3:03
Learn Erlang binary syntax.
Name:
Anonymous2007-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
Learn to use the sizeof operator you stupid fuck instead of doing some elitist faggotry and directly writing the size.
Name:
Anonymous2007-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:
Anonymous2007-11-23 8:52
>>11
Uh, no. Element sizes in a file are determined by the format, not the target runtime.
Plus, there are plenty of languages with compilers written in the language itself. It's not exactly a novel concept anymore.
Name:
Anonymous2007-11-24 3:03
>>23
You can almost be sure that at some point, that compiler/interpreter/whatthefuckingfuckever passed through C. Even if it was just a temporary bootstrapping-type deal.
You can tune a filesystem, but you can't convince motherfuckers that C is fucking superior.
Name:
Anonymous2007-11-24 3:06
>>12
fatal error C1083: Cannot open include file: 'stdint.h': No such file or directory
so much for the C99 standard library.
Name:
Anonymous2007-11-24 4:25
>>24 can't convince motherfuckers that C is fucking superior.
Because it isn't. It's like saying iron and carbon are superior to steel, which is fucking stupid. Core building blocks are a pain in the ass to work with, and are completely unnecessary once you've put them together into something that's actually worth using.
Name:
Anonymous2007-11-24 11:45
>>26
Erlangfag agrees. Hell, aren't there decent binary handling libs for C where you can give it a simple spec? Oh wait, you can't just "simply" give compound data around in C without declaring every bit, which doesn't even map to bits anyway, hence OP's problem...