I am stuck with resize.c for more than a few hours now... I got the headers to work properly with the ~cs50/pset4/peek function but still don't understand why I am getting a weird image a result.

// iterate over infile's scanlines
for (int i = 0; i < inputHeight; i++)
    // iterate over pixels in scanline
    for (int j = 0; j < inputWidth; j++)
        // temporary storage
        RGBTRIPLE triple;

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

        // write n times RGB triple to outfile
        for (int l = 0; l < n; l++)
        fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);

    // go back to the begining of the line we want to duplicate
    fseek(outptr, -outputWidth, SEEK_CUR);

    // temporary storage for the line previously written
    RGBTRIPLE line;

    // read the line just written from outfile
    fread(&line, sizeof(RGBTRIPLE) * outputWidth, 1, outptr);

    for (int l = 0; l < n; l++)
    // write the line n times to outfile
    fwrite(&line, sizeof(RGBTRIPLE) * outputWidth, 1, outptr);

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

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

Any tips on where my logic is flawed? Thanks!!


I see more than one problem. If you fopen'd outptr with the w mode, then the behavior of "reading" from outptr is undefined because the w mode is for "writing". Execute man fopen in the terminal for more info!

Also using outptr for reading and writing could unnecessarily complicate things. I suggest you follow either of the approaches suggested by Zamyla per the walkthrough in the specs!

Second, you're trying to read data of size sizeof(RGBTRIPLE) * outputWidth. Assuming outputWidth is > 1, the data won't fit into line because line is of size RGBTRIPLE only. To fix that, you should probably use an array or you may read and write RGBTRIPLE by RGBTRIPLE.

  • Got it! I went through the video once again and rewrote a bunch of things. It works just fine now. Thanks for the help! May 21 '15 at 23:14

You must log in to answer this question.

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