1

everyone, this is a segment of the code from pset4(whodunit) copy.c, and I'm wondering why the function fseek is needed. Given that padding cannot be read by fread, why not just simply use fputc to add the padding right after the pixels into the output file, and do the next round of the for loop to read the next line from the input file? Why do we bother skipping over?

Thanks!

// iterate over infile's scanlines

for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{

// iterate over pixels in scanline

        for (int j = 0; j < bi.biWidth; j++)
        {
            // temporary storage
            RGBTRIPLE triple;

            // read RGB triple from infile
            fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

            // write RGB triple to outfile
            fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
        }

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

        // then add it back (to demonstrate how)
        for (int k = 0; k < padding; k++)
        {
            fputc(0x00, outptr);
        }
    }
  • Oh, it seems that I got the wrong idea, but the problem is solved and I'm clear with it now, thanks anyway. – Y_C Feb 4 '15 at 14:13
2

Given that padding cannot be read by fread

Who says the padding can't be read by fread? Of course it can and that's the main reason you need to skip it — in order to make fread read the first pixel of the next scanline rather than the padding.

| improve this answer | |

You must log in to answer this question.

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