恢复 cs50:它恢复了 000.jpg 但是当我 运行 它再次没有用 check50 做任何改变。没用?

Recover cs50: It recovered 000.jpg but when i run it again without making any changes with check50. It didnt work?

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


int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        printf("./recover file_name\n");
        return 1;
    }
    
    FILE *file = fopen(argv[1],"r");

    if (file == NULL)
    {
        printf("This file cannot be opened.\n");
        return 1;
    }
    
    //512B is the size of 1 block
    unsigned char buffer[512];
    FILE *img = NULL;
    
    //we alr know that there is 50 images for us to recover
    string filename[50];
    int count = 0;
    
    // read 1 element of 512B into buffer array
    while (fread(buffer, sizeof(unsigned char), 512, file) == 512 *sizeof(unsigned char))
    {
        //NEW jpeg
        if (img == NULL)
        {
            //start of jpeg
            if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
            {
                //create filename(string not array) for new file
                sprintf(filename[count], "%03i.jpg", count);
                
                //create new jpeg file with new filename & write/append in card.raw
                img = fopen(filename[count],"w");
                fwrite(buffer, 512, 1, img);
                fclose(img);
            }
            //continue while loop and read file if cannot find jpeg signature
        }
        else
        {
            //Discover jpeg signature
            //End of jpeg + Start of new jpeg
            if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
            {
                fclose(img);
                //intialize pointer
                img = NULL;
                count++;
                
                //create filename(string not array) for new file
                sprintf(filename[count], "%03i.jpg", count);
                
                //create new jpeg file with new filename & write/append in card.raw
                img = fopen(filename[count],"w");
                fwrite(buffer, 512, 1, img);
            }
            else
            {
                //continue writing to jpeg file
                fwrite(buffer, 512, 1, img);
            }
        }
    }
    
    if (!feof(file))
    {
        printf("Unknown error occured\n");
        return 1;
    }
    
    printf("%i jpeg images recovered\n", count);
    fclose(file);
    return 0;
}

UndefinedBehaviourSanitizer:DEADLYSIGNAL 该信号是由 WRITE 内存访问引起的 Hint:address 指向零页

有什么问题吗?我似乎无法弄清楚。 是逻辑问题吗? 它恢复了 000.jpg 但是当我再次 运行 它时没有对 check50 进行任何更改(至少我认为我没有进行任何更改)。没用??

问题是:只有 string 数据类型将由 get_string 填充时才应使用。我不认为它在课程material中明确提及,但由于内存分配,否则它不会起作用。由于 filename 的 50 个元素没有得到正确分配,将会出现不可预测的结果。

考虑一下:filename 是否需要是一个字符串数组?有什么理由“保留”所有文件名吗?每个将只使用一次,不再需要。如果 filenamechars 的数组,就足够了。它必须用足够的字节来声明以容纳文件名和终止空字节。

虽然此 //we alr know that there is 50 images for us to recover 对于发行版代码中包含的原始文件是正确的,但它是不必要的限制。如果分级程序使用仅包含 10 张照片或 100 张照片的原始文件怎么办?