我已经工作了3个星期试图让这个东西工作,但它不会。我试图通过一次读取512个字节并检查JPG是否从2个可能的JPG开头开始,从.raw文件中“恢复” JPG 。我得到了sorta-work:它在我的文件管理器中显示了.jpg文件,但是当我打开它时它说它是错误的 JPEG 启动!?为什么?我真的很沮丧。老实说,我看不出有什么不妥。我甚至小心翼翼地把它做成了同样的开始!有人能告诉我我做错了什么吗?谢谢你的帮助!
/**
* recover.c
*
* Computer Science 50
* Problem Set 4
*
* Recovers JPEGs from a forensic image.
*/
//0xff 0xd8 0xff 0xe0
//0xff 0xd8 0xff 0xe1
#define BLOCK 512
#define START1END 0xe0
#define START2END 0xe1
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <stdint.h>
void namer();
//sets the begins or jpgs
uint8_t checkjpg1[4] = {0xff, 0xd8, 0xff, 0xe0};
uint8_t checkjpg2[4] = {0xff, 0xd8, 0xff, 0xe1};
//making variables
int found = 0;
char title[BLOCK];
FILE* img;
int ifopen = 1;
int main(int argc, char* argv[])
{
//opening file
FILE* inptr = fopen("card.raw", "r");
//checking if file opening failed
if (inptr == NULL)
{
return 2;
}
//making buffer
unsigned char buffer[BLOCK];
//going through the file
while(fread(&buffer,sizeof(char),BLOCK,inptr) == BLOCK)
{
//checking if begin == the possible begin of jpg
if ((buffer[0] == checkjpg1[0] && buffer[1] == checkjpg1[1] && buffer[2] == checkjpg1[2]) &&
(buffer[3] == checkjpg1[3] || buffer[3] == checkjpg2[3]))
{
//if a jpg is not open
if (ifopen == 1)
{
//make one
found+=1;
namer(&found,&title);
do
{
img = fopen(title,"a");
}
while (img == NULL);
if(buffer[3] == checkjpg1[3])
{
fwrite(&checkjpg1,sizeof(uint8_t),8,img);
}
}
else//else
{
//end the one and open new one
fclose(img);
found +=1;
namer(&found,&title);
do
{
img = fopen(title,"a");
}
while (img == NULL);
if(buffer[3] == checkjpg1[3])
{
fwrite(&checkjpg2,sizeof(uint8_t),8,img);
}
}
}
else if (img != NULL)
{
fwrite(buffer,sizeof(char),BLOCK,img);
}
}
fclose(inptr);
}
void namer(int* found,char* title)
{
if (*found > 9)
{
sprintf(title,"0%d.jpg",*found);
}
else
{
sprintf(title,"00%d.jpg",*found);
}
}