我目前正在研究pset4 recovery.c。我的代码正确地执行了,但是当我运行它时,仅输出两个文件(000.jpg和001.jpg),我相信应该有50个文件。我不确定为什么会这样。打印出的图像非常清晰-前两个图像没有问题。有人对此有任何见识吗?这是我的代码如下:
旁注:我看过一些示例,其中“ while”循环中的条件是“ while(fread(buffer,1,512,card_ptr)== 1)”,其中我的设置为“ while(true)” '。我尝试将while条件更改为此,但未发现任何更改。
typedef uint8_t BYTE;
int main(int argc, char *argv[])
{
//check for eligible command-line arguments
if (argc != 2)
{
printf("Usage: ./recover image\n");
return 1;
}
char *infile = argv[1];
FILE *card_ptr = fopen(infile, "r");
if (card_ptr == NULL)
{
printf("Could not open file\n");
return 1;
}
//declare variables
BYTE buffer[512];
FILE *outfileptr = NULL;
char filename[8];
int jpeg_found = 0;
while (true)
{
outfileptr = fread(buffer, 1, 512, card_ptr);
bool jpgheader = (buffer [0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0);
int counter = 0;
if (jpgheader && outfileptr != NULL)
{
fclose(outfileptr);
counter ++;
}
if (jpgheader)
{
sprintf(filename, "%03i.jpg", counter);
outfileptr = fopen(filename, "w");
}
if (outfileptr != NULL)
{//write to
fwrite(buffer, 1, 512, outfileptr);
}
}
fclose(outfileptr);
fclose(card_ptr);
}