程序在等待退出时超时

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (71)

我不知道是什么让这段代码超时而不退出,我怀疑它与我的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的大小

等等...**

提问于
用户回答回答于

while (counter < factor)循环将永远不会退出,因为你只增加counter当条件counter < (factor - 1)是真实的。

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

扫码关注云+社区

领取腾讯云代金券