Skip to main content
added 112 characters in body
Source Link
wallek876
  • 1.4k
  • 7
  • 15

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

// 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);
        }
    }

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);
    }
}

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);
        }
    }
Source Link
Y_C
  • 115
  • 1
  • 13

fseek in pset4(whodunit)

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);
    }
}