(CS50)致命错误:glibc检测到无效的stdio句柄

时间:2019-10-26 08:35:42

标签: resize cs50

即使使用debug50,我也无法解决此问题。我知道循环会运行一段时间,直到最终到达标题中的错误。该程序停止在我尝试将图像写入文件的位置(在最后一个条件之后)。

CS50上没有其他问题令我震惊,以至于不得不在这里发布。任何帮助,将不胜感激。

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#include "bmp.h"

int main(int argc, char *argv[])
{
    // ensure proper usage
    if (argc != 2)
    {
        printf("Usage: ./recover image\n");
        return 1;
    }

    // remember filenames
    char *infile = argv[1];

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

    BYTE space[512];
    char filename[8];
    FILE *outptr = NULL;
    int imgCount = 0;
    bool search = true;

    while(search == true)
    {
        size_t read = fread(space, sizeof(BYTE), 512, inptr);

        if (feof(inptr))
        {
            search = false;
        }

        if (space[0] == 0xff && space[1] == 0xd8 && space[2] == 0xff && (space[3] & 0xf0) == 0xe0)
        {
            if (outptr != NULL)
            {
                fclose(outptr);
                imgCount++;
            }
            else
            {
                outptr = fopen(filename, "w");
                sprintf(filename, "%03i.jpg", imgCount);
            }
        }

        if (outptr != NULL)
        {
           fwrite(space, sizeof(BYTE), read, outptr);
        }
    }

    // close outfile
    fclose(outptr);

    // close infile
    fclose(inptr);

    // success
    return 0;
}

谢谢。

0 个答案:

没有答案