Bitmap to a Structure question.

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Musi
Posts: 106
Joined: November 25th, 2012, 1:06 am

Bitmap to a Structure question.

Post by Musi » November 25th, 2012, 9:00 pm

I've been following lesson 20 and I just wanted to understand how the bytes of a Bitmap file are put into a structure, I'm not really getting my head around it.

Code: Select all

FILE* bmpFile = fopen( fileName, "wb" );
So here fopen takes the name of a file on the hard drive and copies the file into the space bmpFile points to in RAM?

then here

Code: Select all

BitmapFileHeader fileHeader;
fread( &fileHeader,sizeof( fileHeader ),1,bmpFile );
fread copies bytes from RAM that bmpFile points to, storing them in the members of fileHeader until it has copied the same amount that sizeof( fileHeader ) returns?

am i right so far? i just want to know how the bytes are stored in the right members, also whats the third argument of fread for?
Musi

There are 10 types of people that understand binary.
Those that do, and those that don't.

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Bitmap to a Structure question.

Post by chili » November 27th, 2012, 1:45 pm

fopen doesn't copy any data. All it does is tell the OS to open the file and get it ready for being read from/written to.

fread copies the bytes from the disk to the ram that holds fileHeader. The bytes are stored in the right members because the structure is set up such that the members are inthe same order as they are on the disk. If you change the order of the members in the structure definition, you will mess it all up.

The third argument specifies how many to copy. For example, if you have an array of 1000 ints you want to read in, you would do: fread( myArray,sizeof( int ),1000,myFile );
Chili

Musi
Posts: 106
Joined: November 25th, 2012, 1:06 am

Re: Bitmap to a Structure question.

Post by Musi » November 27th, 2012, 4:48 pm

thanks for clearing that up chili,

Just curious, so if you had an array of 1000 ints would this
fread( myArray,sizeof( myArray ),1,myFile );
do the same as this
fread( myArray,sizeof( int ),1000,myFile ); ?

Or does that only work for a structure?
Musi

There are 10 types of people that understand binary.
Those that do, and those that don't.

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: Bitmap to a Structure question.

Post by LuX » November 27th, 2012, 7:32 pm

Yeah, as long as the int array is defined with a static size. Something like "int *myArray" wouldn't work but "int myArray [25]" is fine.
ʕ •ᴥ•ʔ

Musi
Posts: 106
Joined: November 25th, 2012, 1:06 am

Re: Bitmap to a Structure question.

Post by Musi » November 27th, 2012, 8:24 pm

Thanks Lux.

You know, i think i'm actually learning stuff :shock:.
Musi

There are 10 types of people that understand binary.
Those that do, and those that don't.

Post Reply