我的终端在运行C程序后冻结

时间:2020-08-14 10:17:10

标签: c cs50

我要做什么?

我正在尝试解决CS50中的recover问题,就像这样...

为您提供了一个损坏的文件,怎么办?好吧,该文件应该是多个JPG文件,而不是一个大文件。您的任务是将所述文件分成多个JPG,为此,您必须遍历原始文件中的字节并找到此0xff 0xd8 0xff和一个介于0xe00xef之间的数字...这些数字是什么意思?好了,它们指示了JPG的开始,因此您必须根据该信息来创建和关闭文件...

问题很长,因此请按this了解更多信息。

我做了什么?

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <cs50.h>

//create new data type BYTE
typedef uint8_t BYTE;

int main(int argc, char *argv[])
{
    //variables
    int counter = 0;
    char name[8];
    //create new array of BYTES, it is 512 because as you can see later I am reading the input file in 512 byte chunks
    BYTE buffer[512];
    int bytes = 512;
    //output image
    FILE* img;
    //input image
    FILE* f;

    //checks if the arguments passed are wrong
    if (argc != 2)
    {
        printf("Usage: ./recover <file>");
        return 1;
    }
    
    //opens the file passed in the arguments 
    f = fopen(argv[1], "r");
    
    //checks if file is null
    if (f == NULL)
    {
        printf("bruh");
        return 1;
    }
    
    //loop till you reach zero
    //also read in 512 bytes chunks from the input file and store to buffer
    while (fread(buffer, bytes, 1, f))
    {
        //checks if this is a jpg, and if it is checks if it is the first one
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0 && counter == 0)
        {
            //assigns new value to name
            sprintf(name, "%03i.jpg", counter);
            //creates new file with the name we've just created
            img = fopen(name, "w");
            //write to that file
            fwrite(buffer, bytes, 1, img);
            //increment number of files by 1
            counter++;
        }
        //checks if it encoutered another jpg, if it did it will close the last created file and create a new one
        else if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        {
            //closes last file
            fclose(img);
            //assigns new value to name depending on how many images were printed
            sprintf(name, "%03i.jpg", counter);
            //creates new file with the name we've just created
            img = fopen(name, "w");
            //writes to that file
            fwrite(buffer, bytes, 1, img);
            //increment number of files by one
            counter++;
        }

        //if no jpgs are encountered, continue to write to the last file
        else
        {
            fwrite(buffer, bytes, 1, img);
        }
    }
    //close opened files
    fclose(f);
    fclose(img);
    
    return 0;
}

如果您认为我在不告诉您我做了什么的情况下向您转储了一段代码,那么我已注释掉每一位。

出了什么问题?

根据帖子的标题,当我运行程序并传递一些参数时...终端只是冻结,什么也没有出现...从来没有错误。

如果您想复制此问题:

  • 运行wget https://cdn.cs50.net/2019/fall/psets/4/recover/recover.zip 在您的终端中并解压缩文件...

  • 然后键入make recover进行编译(如果不起作用,请使用
    通常会发出叮当声)

  • 最后键入./recover card.raw来运行程序...应该生成大约50个文件,但同样不会

1 个答案:

答案 0 :(得分:2)

即使没有合适的标题,您的最后一个else也可能发生 之前发现在这种情况下,您的img文件可能不是 在蜜蜂使用之前打开。

首先必须正确初始化

FILE* img=NULL; // you forgot this initialisation

然后,在最后一个else中,您可以测试文件是否真正打开。

if(img!=NULL) // you forgot this test
{
  fwrite(buffer, bytes, 1, img);
}

在我的计算机上,您的初始程序仅在分段时停止 因为偶然,即使没有明确初始化, img恰好是NULL

我猜想如果fwrite()使用未初始化的img指向 到任意但可访问的内存,它可以偶然解释 此未定义/未初始化的FILE与标准相关 输入/输出;然后在标准I / O上发送任意字节会冻结 终端...
但是没有什么是不确定的...

相关问题