cs50 恢复,"recovered images do not match"
cs50 recover, "recovered images do not match"
我的源码编译成功,共生成了50张图片
但是,none 的恢复图像与原始图像匹配。
所有的 jpeg 格式如下所示。
如您所见,它们似乎有奇怪的边缘重叠。
有些看起来还可以,但还是和原图不符
如果您能提供有关如何调试的任何见解,请告诉我。
这是我的代码
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Usage: ./recover image\n");
return 1;
}
FILE *file = fopen(argv[1], "r");
if (file == NULL)
{
printf("Could not open %s.\n", argv[1]);
return 1;
}
typedef uint8_t BYTE;
BYTE buffer[512];
char *filename[8];
int jpeg_counter = 0;
bool foundStartOfJPEG = false;
FILE *img;
// read memory card until the end of file
while(fread(buffer, sizeof(BYTE) * 512, 1, file) == 1)
{
// if buffer has a signature of JPEG file,
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && ((buffer[3]) & 0xf0) == 0xe0)
{
if (foundStartOfJPEG == true)
{
fclose(img);
jpeg_counter += 1;
}
foundStartOfJPEG = true;
// create a file with index
sprintf(*filename, "%03i.jpg", jpeg_counter);
// open that file to write into it
img = fopen(*filename, "w");
// write the block of memory (buffer), to that file
fwrite(buffer, sizeof(buffer), 1, img);
}
else if (foundStartOfJPEG == true)
{
fwrite(buffer, sizeof(buffer), 1, img);
}
}
fclose(file);
return 0;
}
我尝试了您的代码,当您将文件名从指针更改为数组(即 char *filename 到 char 文件名)时它起作用了。
这也适用于指针的使用。这里 p 是指向文件名的指针。但是,我认为它的使用是多余的。如果根本不使用 p,您可以说文件名是指向文件名第一个元素的指针,即文件名 [0]。因此,当您使用 char *filename[8] 时,就像说文件名是指向指针的指针……希望这是有道理的!
typedef uint8_t BYTE;
BYTE buffer[512];
char filename[8];
char *p = filename;
int jpeg_counter = 0;
bool foundStartOfJPEG = false;
FILE *img;
// read memory card until the end of file
while(fread(buffer, sizeof(BYTE) * 512, 1, file) == 1)
{
// if buffer has a signature of JPEG file,
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && ((buffer[3]) & 0xf0) == 0xe0)
{
if (foundStartOfJPEG == true)
{
fclose(img);
}
jpeg_counter += 1;
foundStartOfJPEG = true;
// create a file with index
sprintf(p, "%03i.jpg", jpeg_counter);
// open that file to write into it
img = fopen(p, "w");
// write the block of memory (buffer), to that file
fwrite(buffer, sizeof(buffer), 1, img);
}
else if (foundStartOfJPEG == true)
{
fwrite(buffer, sizeof(buffer), 1, img);
}
}
我的源码编译成功,共生成了50张图片
但是,none 的恢复图像与原始图像匹配。
所有的 jpeg 格式如下所示。
如您所见,它们似乎有奇怪的边缘重叠。
有些看起来还可以,但还是和原图不符
如果您能提供有关如何调试的任何见解,请告诉我。
这是我的代码
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Usage: ./recover image\n");
return 1;
}
FILE *file = fopen(argv[1], "r");
if (file == NULL)
{
printf("Could not open %s.\n", argv[1]);
return 1;
}
typedef uint8_t BYTE;
BYTE buffer[512];
char *filename[8];
int jpeg_counter = 0;
bool foundStartOfJPEG = false;
FILE *img;
// read memory card until the end of file
while(fread(buffer, sizeof(BYTE) * 512, 1, file) == 1)
{
// if buffer has a signature of JPEG file,
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && ((buffer[3]) & 0xf0) == 0xe0)
{
if (foundStartOfJPEG == true)
{
fclose(img);
jpeg_counter += 1;
}
foundStartOfJPEG = true;
// create a file with index
sprintf(*filename, "%03i.jpg", jpeg_counter);
// open that file to write into it
img = fopen(*filename, "w");
// write the block of memory (buffer), to that file
fwrite(buffer, sizeof(buffer), 1, img);
}
else if (foundStartOfJPEG == true)
{
fwrite(buffer, sizeof(buffer), 1, img);
}
}
fclose(file);
return 0;
}
我尝试了您的代码,当您将文件名从指针更改为数组(即 char *filename 到 char 文件名)时它起作用了。
这也适用于指针的使用。这里 p 是指向文件名的指针。但是,我认为它的使用是多余的。如果根本不使用 p,您可以说文件名是指向文件名第一个元素的指针,即文件名 [0]。因此,当您使用 char *filename[8] 时,就像说文件名是指向指针的指针……希望这是有道理的!
typedef uint8_t BYTE;
BYTE buffer[512];
char filename[8];
char *p = filename;
int jpeg_counter = 0;
bool foundStartOfJPEG = false;
FILE *img;
// read memory card until the end of file
while(fread(buffer, sizeof(BYTE) * 512, 1, file) == 1)
{
// if buffer has a signature of JPEG file,
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && ((buffer[3]) & 0xf0) == 0xe0)
{
if (foundStartOfJPEG == true)
{
fclose(img);
}
jpeg_counter += 1;
foundStartOfJPEG = true;
// create a file with index
sprintf(p, "%03i.jpg", jpeg_counter);
// open that file to write into it
img = fopen(p, "w");
// write the block of memory (buffer), to that file
fwrite(buffer, sizeof(buffer), 1, img);
}
else if (foundStartOfJPEG == true)
{
fwrite(buffer, sizeof(buffer), 1, img);
}
}