首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >程序在等待退出时超时

程序在等待退出时超时
EN

Stack Overflow用户
提问于 2019-06-26 08:30:29
回答 1查看 1.7K关注 0票数 0

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

代码语言:javascript
复制
// 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 iterate 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 :)大小.c和bmp.h存在。

:) resize.c编译。

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

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

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

以此类推...**

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56763634

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档