我正在尝试从 SD 卡 FAT32 中恢复数据,因为我知道当我在.jpg
那里找到它时,它会按顺序在那里直到文件结束。现在该程序能够恢复 1 jpeg 但停止。我该如何改进它,或者有人可以指出我的逻辑缺陷吗?
也在: http: //pastebin.com/Rak6L5Z0
编辑:卡片文件 http://www.mediafire.com/?et8pvhvacpy9kv1
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef uint8_t BYTE;
#define BLOCKSIZE 512
int main(void)
{
// define 512 byte block
BYTE block[BLOCKSIZE];
// open memory card
FILE* fp = fopen("card.raw", "r");
// check if file opened
if (fp == NULL)
{
printf("Error opening file\n");
return 1;
}
// open output file
FILE* outfile;
outfile = NULL;
int num = 0;
char filename[14];
int x = 0;
int y = 1;
// main block till end of file
while (y == 1)
{
if ( x == 0)
{
// read every 512 block
fread(&block, sizeof(block), 1 , fp);
}
// if 4 first characters jpg
if ((block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff && block[3] == 0xe0) || (block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff && block[3] == 0xe1))
{
sprintf(filename, "%03d_output.jpg", num);
outfile = fopen(filename, "a");
fwrite(&block, sizeof(block), 1, outfile);
//fseek(fp, sizeof(block), SEEK_CUR);
fread(&block, sizeof(block), 1 , fp);
while (!((block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff && block[3] == 0xe0) || (block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff && block[3] == 0xe1)))
{
fwrite(&block, sizeof(block), 1, outfile);
y = fread(&block, sizeof(block), 1, fp);
}
//fseek(fp, -(sizeof(block)), SEEK_CUR);
fclose(outfile);
num++;
x = 1;
//y = fread(&block, sizeof(block), 1, fp);
}
//printf("%s\n", block);
}
// close memory card
fclose(fp);
return 0;
}