程序在等待退出时超时

时间:2019-06-26 00:30:28

标签: c cs50

我不确定是什么原因导致此代码超时而没有退出,我怀疑这与代码末尾的while循环有关

// Copies a BMP file and resizes it

#include <stdio.h>
#include <stdlib.h>
#include "bmp.h"

int main(int argc, char *argv[])
{
    // ensure proper usage
    if (argc != 4)
    {
        fprintf(stderr, "Usage: ./resize factor infile outfile\n");
        return 1;
    }
    // Check argument 1 to see if integer within aceptable range
    int factor = atoi(argv[1]);
    if (factor <= 0 || factor > 100)
    {
        fprintf(stderr, "Must be a positive integer greater than 0 and eqaul or less than 100\n");
        return 1;
    }

    // remember filenames
    char *infile = argv[2];
    char *outfile = argv[3];

    // open input file
    FILE *inptr = fopen(infile, "r");
    if (inptr == NULL)
    {
        fprintf(stderr, "Could not open %s.\n", infile);
        return 2;
    }

    // open output file
    FILE *outptr = fopen(outfile, "w");
    if (outptr == NULL)
    {
        fclose(inptr);
        fprintf(stderr, "Could not create %s.\n", outfile);
        return 3;
    }

    // read infile's BITMAPFILEHEADER
    BITMAPFILEHEADER bf;
    BITMAPFILEHEADER bf_New;
    fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
    bf_New = bf;

    // read infile's BITMAPINFOHEADER
    BITMAPINFOHEADER bi;
    BITMAPINFOHEADER bi_New;
    fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
    bi_New = bi;

    // ensure infile is (likely) a 24-bit uncompressed BMP 4.0
    if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
        bi.biBitCount != 24 || bi.biCompression != 0)
    {
        fclose(outptr);
        fclose(inptr);
        fprintf(stderr, "Unsupported file format.\n");
        return 4;
    }

    // set new height and width of BMP
    bi_New.biHeight = bi.biHeight * factor;
    bi_New.biWidth = bi.biWidth * factor;

    // calculate padding for old file and new file
    int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;
    int padding_New = (4 - (bi_New.biWidth * sizeof(RGBTRIPLE)) % 4) % 4;

    // set the file size for the new file
    bf_New.bfSize = 54 + (bi_New.biWidth * sizeof(RGBTRIPLE) + padding_New) * abs(bi_New.biHeight);
    bi_New.biSizeImage = bf_New.bfSize - 54;


    // write outfile's BITMAPFILEHEADER
    fwrite(&bf_New, sizeof(BITMAPFILEHEADER), 1, outptr);

    // write outfile's BITMAPINFOHEADER
    fwrite(&bi_New, sizeof(BITMAPINFOHEADER), 1, outptr);

    // 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++)
        {
            // intialise counter to print rows by amount of the factor
            int counter = 0;

            // while loop to keep continuing until factor is less than or equal to counter
            while (counter < factor)
            {
                // iterate over pixels in scanline
                for(int k = 0; k < bi.biWidth; k++)
                {
                    // temporary storage
                    RGBTRIPLE triple;

                    // declare pixel counter
                    int pixel_counter = 0;

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

                    // write RGB triple to outfile and use a while loop to itterate the same pixel by factor times
                    while (pixel_counter < factor)
                    {
                        fwrite(&triple, sizeof(RGBTRIPLE), 1, outptr);
                        pixel_counter++;
                    }
                }
                // add new padding
                for (int l = 0; l < padding_New; l++)
                {
                    fputc(0x00, outptr);
                }

                // seek back to the beginning of row in input file, but not after iteration of printing
                if (counter < (factor - 1))
                {
                    fseek(inptr, -(bi.biWidth * sizeof(RGBTRIPLE)), SEEK_CUR);
                    counter++;
                }
            }
        }

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

    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // success
    return 0;
}

**这些是我从我的课程代码检查器CS50中收到的错误消息 :)存在resize.c和bmp.h。

:) resize.c编译。

:(当n为1时不调整small.bmp的大小     在等待程序退出时超时

:(当n为2时正确调整small.bmp的大小     在等待程序退出时超时

:(当n为3时正确调整small.bmp的大小     在等待程序退出时超时

以此类推... **

1 个答案:

答案 0 :(得分:1)

while (counter < factor)循环永远不会退出,因为只有在条件counter为真时,您才递增counter < (factor - 1)

您可能想在counter条件之外增加if (counter < factor - 1)