Hi there!
I´m triyng to copy the header of a BMP file into a defined struct inside my code.
This header has this format:
int (6 var) 1st-->6th struct member unsigned int (2 var) 7th-->8th struct member int(4 var) 9th-->13th struct member
In order to use the fread function, if I do:
fread(&cab, sizeof(cab),1, file)
,its not right, 'cause the two unsigned int struct members (2 bytes) will be copyed like an int (4 bytes).
Is there any way to do the fread changing the size? Something like:
-From 1st member to 6th, take 4 bytes.
-From 7th to 8th, take 2 bytes.
-From 9th to 13th, again 4 bytes.
And I'm not alone. Lots of people still prefer stdio in some situations.
Of course there's a list of reasons not to use stdio. And I'm not saying anyone should use stdio, or that stdio is better than iostream. I'm just saying that it's part of C++.
I guess all I'm saying is that if the goal was to discourage the use of stdio (which, again, I agree is an admirable goal), then it's better to explain the facts and reasons why stdio should be discouraged.
"it has type safety issues"
"it's more susceptible to buffer overflows"
"it doesn't work with std::strings"
etc
All would have been better responses than "it's not C++", which is a generic (and false) blanket statement.
Edit: Hm, just read the wikipedia struct again. I hate it. Use this:
#pragma pack(1)
struct BMPHeader {
char magic[2];
unsigned int size;
char reserved[4];
unsigned int offset;
};
That header sucks. With it, you dont get any information about the bmp file, such as if its True Color or Grey Scale. ;).
I finally fixed the problem typing short int instead of unsigned int. But I don´t know why that way works. I always thought short int and unsigned int have the same bytes lenght (2 bytes) o_0
That header sucks. With it, you dont get any information about the bmp file, such as if its True Color or Grey Scale. ;).
Pff.. Idk. ;)
As long as you keep the #pragma pack thing, you'll be fine.
You can try out the sizes of your compiler with this:
1 2 3 4 5 6 7
int main()
{
cout << sizeof(int) << endl; // most probably 4
cout << sizeof(long) << endl; // typically 4 too
cout << sizeof(short) << endl; // usually 2
cout << sizeof(char) << endl; // if its not 1, sue your compiler vendor
}