2

I don't really understand the following code in copy.c:

// write outfile's BITMAPFILEHEADER
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);

// write outfile's BITMAPINFOHEADER
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);

Does this code actually write out the header information or does it just skip the space they would otherwise occupy in bytes?

Then how is how is information such as bi.biWidth updated if the header is already written?

How do I update information such as bi.biWidth?

2
  • did you try man fwrite? also what makes you think the value in bi.biWidth should be updated in copy.c?
    – kzidane
    Commented Jul 30, 2015 at 3:39
  • You know, Kareem, I missed that. I was thinking this was about resize. ;-) It will still be useful later.
    – Cliff B
    Commented Jul 30, 2015 at 3:58

1 Answer 1

4

In your example, bf and bi are pointers to two different structures that hold several variables each. These commands will actually write out the indicated full header structures out to the files. Let's walk through one of them:

// write outfile's BITMAPFILEHEADER
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);

This command says to write out to the file pointed to by outptr at the current location set in outptr. It will write 1 block of size sizeof(BITMATFILEHEADER), the size of the data in the header structure. The source of the data to be written is the contents pointed to by bf, or &bf. The file pointer will then be repositioned to the first byte after the amount of data specified in the command.

How is header data updated after it is written out by these commands? Good question! It's not impossible, it could be done (I'm not about to explain how here), but it would really, really be a whole lot simpler if you update the data in the header structures in program memory BEFORE you execute these write commands!

How do you update individual header fields? It's simple. Earlier, you fread() into the two header structures, bi and bf`. That means that all the individual fields are there. All you need to do is reassign the individual fields, just like any other int variable, like this:

bi.biWidth = 100;

You can use any kind of simple or complex assignment statement that you'd use with any int. It's standard selection of a field in a structure. If you don't understand structures and how to use them, you should review the class videos and shorts on structs.

If this answers your question, please accept this answer to remove the question from the unanswered question pool. Let's keep up on forum housekeeping. ;-)

1
  • That makes so much sense! I'm getting a better idea now how to structure my program. :D Commented Jul 30, 2015 at 3:53

You must log in to answer this question.

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