I am currently stuck on this problem set, as it seems like I am able to recover the 50 jpgs but I am failing the check50. I am not sure what I am doing wrong and would appreciate some help.
:( recovers 000.jpg correctly
000.jpg not found
:( recovers middle images correctly
001.jpg not found
:( recovers 049.jpg correctly
049.jpg not found
It also seems like the jpgs i have recovered are corrupted. This is my code for the problem:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main(int argc, char *argv[])
{
if (argc!=2 )
{
printf("Usage: ./recover image\n");
return 1;
}
FILE *f= fopen(argv[1], "r");
if (!f)
{
printf("File cannot be opened\n");
return 1;
}
int count=0;
typedef uint8_t BYTE;
//make a buffer of size 512b
BYTE buff[512]={0};
//allocate memory for filenamed pic
char *pic=malloc(sizeof(char)*2048);
//intialise file pointer img
FILE *img= NULL;
while(fread(buff, 512, 1, f))
//condition fulfilled to be a jpeg
{
if(buff[0]==0xff && buff[1]==0xd8 && buff[2]==0xff && ((buff[3]&0xf0)==0xe0) )
{
if((count=0))
{
sprintf(pic, "%03i.jpg", count);
img=fopen(pic, "w");
fwrite (buff, 512, 1, img);
fclose(img);
count++;
}
else if (count>0)
{
count++;
sprintf(pic, "%03i.jpg", count);
img=fopen(pic, "w");
fwrite (buff, 512, 1, img);
fclose(img);
}
}
//if not start of jpeg
else
{
if(count>0)
{
fwrite(buff,sizeof(buff),1,img);
fclose(img);
}
}
}
free(pic);
}