【问题标题】:Recover cs50: It recovered 000.jpg but when i run it again without making any changes with check50. It didnt work?恢复 cs50:它恢复了 000.jpg,但是当我再次运行它而不用 check50 进行任何更改时。它没有工作?
【发布时间】:2020-07-18 07:10:08
【问题描述】:
#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 内存访问引起的 提示:地址指向零页

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

【问题讨论】:

    标签: cs50 recover


    【解决方案1】:

    问题是:如果get_string 填充数据类型,则应该只使用string 数据类型。我认为课程材料中没有明确提到它,但是由于内存分配,否则它将无法正常工作。由于filename的50个元素没有得到正确分配,会出现不可预知的结果。

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

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

    【讨论】:

    • 非常感谢!我不知道问题出在字符串部分。
    猜你喜欢
    • 2020-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多