0
// skip over padding, if any
            fseek(inptr, padding, SEEK_CUR);

             // then add it back (to demonstrate how)
            for (int k = 0; k < old_padding; k++)
            {
                fputc(0x00, outptr);
            }

If someone could:

  • clarify what the fseek() function does and

  • why the for loop needs the old padding, rather than the new padding

1
  • Did you watch Zamyla's video?
    – leanne
    Commented Aug 14, 2019 at 15:43

2 Answers 2

0

my biggest tip for understanding the code in copy.c is to go through the program and rename items to more readable words for you. Dont worry about saving keystrokes when you dont understand the code. The strange nomenclature can make it very difficult to remember what each item is. Small things like taking inptr and changing it to InputFile can make it easier to read at first. bi.blahblah is harder to understand/remember what it is than strctBitmapInfoHeader.blahblah and strctBitMapFileHeader.blahblah

You are on the right track with the padding. Since you copied that directly over from copy.c, maybe it doesn't need to be in the same location or used in the exact same way. Using the walkthroughs pseudocode try and think which padding will have to be added to the new file. Think about when that should happen.

When reading a file using fread a "cursor" is placed in the file being read. fseek moves that cursor around. It takes 3 arguments, the first is the file pointer(in this case the original bitmap), the second is how many positions the cursor will move(depending on how you want the cursor to move in the file, this can be a positive or negative number), and the third is where the cursor will move from(the beginning of the file, the current location, or the end of the file)

0

This is code from the original copy.c program that will later be used in resize.

An fseek call will cause the file pointer to change position in a file. You should google it and study the details.

In this case, the fseek call will cause the file pointer for the input file to skip over the padding in the input file. If you were just copying the data from input file to output file, this wouldn't be necessary. You could just keep copying data. BUT, in resize, the padding size of the input file may be different than the output file.

So, when outputting the new file's padding, no matter what size, you need to write the correct number of bytes for the padding to the output file. In the loop, the fputc call writes out exactly 1 byte, while the for loop controls how many times that happens, thus writing out the correct number of bytes.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

0

You must log in to answer this question.

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