为什么会出现段错误

时间:2019-01-17 15:09:11

标签: std stdio

我已经尝试了很多方法来解决此段错误的问题,我不确定发生了什么问题,因为据我了解,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);

1 个答案:

答案 0 :(得分:0)

您在fread之前砸碎了该区块中的堆叠物

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

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