0

I've just solved the resizing part of pset5 without completely understanding what is happening during the reading of the infile and writing of the outfile bit. To clarify my question, here is my code snippet that I have a question about:

// iterate over infile's scanlines
for (int i = 0, biHeight = abs(bi.biHeight); i < biHeight; i++)
{

    // temporary storage as array of structs
    RGBTRIPLE temp_line[bi2.biWidth];

    // iterate over pixels in scanline
    for (int j = 0; j < bi.biWidth; j++)
    {
        // temporary storage
        RGBTRIPLE triple;

        // read pixels of infile
        fread(&triple, sizeof(RGBTRIPLE), 1, inptr);

        for (int k = 0; k < arg1; k++)
        {
            temp_line[(arg1 * j) + k] = triple;
        }     
    }

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

    for (int l = 0; l < arg1; l++)
    {
        fwrite(&temp_line, sizeof(temp_line), 1, outptr);

        for (int k = 0; k < padding2; k++)
        {  
            fputc(0x00, outptr);
        }
    }                        
}

Now, my question is: How does the programme know which parts of the infile it has to read at different steps of the outermost for loop? At i = 0, it will start at the beginning of the infile's bitmap, read along its first line, and in my code store the RGBTRIPLES in an array of structs before writing it n times to the outfile (plus padding). What I don't understand is, when it comes to i = 1, how is it that the fread function in the second for loop does not again start at the beginning of the first but of the second line of the infile?

Thanks!

1

To answer your question:

What I don't understand is, when it comes to i = 1, how is it that the fread function in the second for loop does not again start at the beginning of the first but of the second line of the infile?

This is because the fread function also updates the cursor every time fread is called. This cursor is stored independently of the for loop, so it is not getting reset every time, but rather constantly updated, just like the cursor in a word processor would as you scan through lines.

| improve this answer | |
  • Thanks, I kinda figured it would be something along those lines.. – pat_krat Sep 2 '14 at 21:19

You must log in to answer this question.

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