I managed to complete the pset4/resize, but am having difficulty understanding the concept behind how it works. That gap is showing up in my attempt at pset4/jpg.

fread as far as I know doesn't tell the computer where to begin getting the data(e.g. the 40 bytes in BITMAPFILEHEADER, which begins after byte 14).

Is it by the datatype, BITMAPFILEHEADER?.. if so, are we just lucky microsoft specified and named the datatype for us..?

What if we are trying to do the same for Jpg, but they don't have structs pre-defined like microsoft did for .bmp files. How do I create, then access them?

To sum it up, my question is: How does the structs declared in 'bmp.h' know which bytes to get the data from?

2 Answers 2


How does the structs declared in 'bmp.h' know which bytes to get the data from?

The resize program works with the BMP file format which has a few characteristics which need to be altered, namely headers containing information about the data and padding.

Supporting this notion, there are a few structures created beforehand in bmp.h. They are simply collections of data types. You can view these data types in the same bmp.h, named BYTE, WORD, LONG, DWORD.

The structs declared in bmp.h do not know beforehand where to get the data. The data is loaded into variables of the type BITMAPINFOHEADER and BITMAPFILEHEADER.

So, consider the following block of code

FILE* inptr = fopen(infile, "r");
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);

Here, I open the file, define a BITMAPFILEHEADER, then load some bytes into the bf variable.

Where do I start reading? The start of the file.

How many bytes do I load into bf? You can examine the arguments of the fread function to answer that question.

What happens if I fread some more bytes and the file was not closed? It will start loading from the previous position of the file pointer left by the previous fread function.

Since you don't need to resize the jpeg's, you can do without header information and simply load some blocks of data into memory. How much data? You need to examine the program specification for that!


fread does really tell the computer where to start getting the data:

// read infile's BITMAPFILEHEADER
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);

// read infile's BITMAPINFOHEADER
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);

The first fread reads sizeof(BITMAPFILEHEADER) bytes from the beginning of inptr. It knows to read 14 bytes because that is how many bytes are defined in the struct definition. Now the current file pointer for inptr is at the fifteenth byte. The second fread reads sizeof(BITMAPINFOHEADER) bytes. Which is defined in bmp.h as 40 bytes.

What MS "defined" is the meaning of the first 54 bytes. The program author defined the structs to match the file layout to make it easier to access and manipulate the data.

  • 1
    I see, so.. the sequence of the structs in my 'bmp.h' file doesn't matter. Also, if I fread BITMAPINFOHEADER first, 'fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);' then it'll start reading 40bytes from the beginning of the file. I think that' cleared a few things up for me, thanks! Commented Dec 2, 2016 at 16:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .