为什么这会导致段错误

Why does this fread segfault

我已经尝试了很多方法来解决这个段错误问题,我不确定发生了什么错误,因为根据我的理解,fread 行不应该是段错误

// ensure proper usage
    if (argc != 2)
    {
        fprintf(stderr, "Usage: ./recover file");
        return 1;
    }

    char* recover = argv[1];
    // open input file
    FILE * raw_file = fopen(recover, "r");
    if (raw_file == NULL)
    {
        fprintf(stderr, "Could not open %s.\n", recover);
        return 2;
    }

    //somehow read the file

    int counter = 1;
    char file[2];
    sprintf(file,"%03i.jpg",counter);

    int buffer[512];

    //read file and put into buffer
    int*bf = malloc(sizeof(int));
    fread(bf, sizeof(int), 1, raw_file);

你在 fread

char file[2];
sprintf(file,"%03i.jpg",counter);

file 太小了,无法容纳要格式化的字符数。