【发布时间】: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 进行任何更改时(至少我认为我没有进行任何更改)。没用??
【问题讨论】: