fread

Mar 26, 2010 at 7:14pm
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.

Thanks, I hope it´s understandable ;)
Last edited on Mar 26, 2010 at 7:23pm
Mar 26, 2010 at 9:47pm
Hey, please I need help.
It´s next week work stuff...
Ask me if u dont understand my doubt.
thanks
Mar 27, 2010 at 9:48am
Up...
Does anybody know something about file handling in c++? :/
Mar 28, 2010 at 12:26am
fread() is not C++.

http://www.cplusplus.com/doc/tutorial/files/

You will probably want to open it in binary mode.
Mar 28, 2010 at 2:17am
fread() is not C++.


How do you figure? It's part of the standard lib. It compiles. Sounds like C++ to me.
Mar 28, 2010 at 2:34pm
files handling is not C++? ¬_¬
Is this forum just for compiling the "Hello World" code?

PS: any medium C++ user knows how 2 use fstream library... Not just volatile memory. Try to improve ur programming knowledge, d00d.
Mar 28, 2010 at 10:21pm
It's part of the standard lib. It compiles. Sounds like C++ to me.
It's from the C standard library
Mar 28, 2010 at 11:16pm
It's from the C standard library


And is also part of the C++ standard library.

What about sqrt, pow, rand, etc? Are they also "not C++"?

Try to improve ur programming knowledge, d00d.


Was that directed at me? I constantly try and frequently do.

I never said you should use FILE over fstream. Zhuge said FILE was not C++, and I disagreed.
Mar 29, 2010 at 12:52pm
It will work with C++ bit it's not the C++ way
Mar 29, 2010 at 2:45pm
Don't you mean it's not the OOP way?

C++ doesn't have to be OOP though, right? It seems like everyone on these boards is always talking about how C++ is a multi-paradigm language:

http://cplusplus.com/forum/beginner/4256/#msg18755
http://cplusplus.com/forum/general/12321/page2.html#msg59482
http://cplusplus.com/forum/general/8193/#msg37897
http://cplusplus.com/forum/beginner/20800/#msg109633


Type safety issues aside, I still find fprintf tons easier to use the fstreams (even though I don't really use it any more)

1
2
3
fprintf(myfile,"0x%04X",mynum);
// vs.
myfile << "0x" << setw(4) << setfill('0') << hex << uppercase << mynum;


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.
Mar 29, 2010 at 2:54pm
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


Really, guy.. Wikipedia.. they even got a C-struct declaration of BMP headers over there.

Ciao, Imi.

Edit: Hm, just read the wikipedia struct again. I hate it. Use this:
1
2
3
4
5
6
7
#pragma pack(1)
struct BMPHeader {
    char magic[2];
    unsigned int size;
    char reserved[4];
    unsigned int offset;
};

Last edited on Mar 29, 2010 at 3:02pm
Mar 29, 2010 at 2:54pm
I mean that it's there only because C++ had to be compatible with C. If you want to use files in C++ the right way is via streams
Mar 29, 2010 at 4:30pm
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
Mar 29, 2010 at 6:04pm
int without specifier can be either short or long
Mar 29, 2010 at 10:33pm
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
}


Ciao, Imi.
Topic archived. No new replies allowed.