In Pset4 everything is going fine, but recover.c, which inside the IDE works pretty well, recovering all the 50 images in card.raw flawlessly, fails miserably with check50.
when I run it through Check50 (check50 2015.fall.pset4.recover recover.c), it returns the following
:) recover.c exists
:) recover.c compiles
:( recovers 000.jpg correctly
\ killed by server
:( recovers middle files correctly
\ killed by server
:( recovers last file correctly
\ killed by server
Also please note that that the size of jpgs recovered and the size of card.raw is 17094144 and 17095168 respectively i.e., there is a difference of 1kb exact between the two.
Please find my code below :
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <cs50.h>
typedef uint8_t BYTE;
int main(int argc, char* argv[]){
char* infile = "card.raw";
char name[10];
BYTE sign[4];
BYTE mem[512];
int d;
int counter = -1;
FILE* img = fopen(infile,"r");
if(img == NULL){
printf("Could not open file %s",infile);
return 1;
}
do{
counter++;
sprintf(name,"%03d.jpg",counter);
FILE* photo = fopen(name,"w");
if(photo == NULL){
fclose(img);
fprintf(stderr, "Could not create %s.\n", name);
return 2;
}
fread(sign,4,1,img);
while(!((sign[0] == 0xff) && (sign[1] == 0xd8) && (sign[2] == 0xff) && (sign[3] >= 0xe0) && (sign[3] <= 0xef))){
fseek(img,-3,SEEK_CUR);
fread(sign,4,1,img);
}
do{
fseek(img,-4,SEEK_CUR);
d = fread(mem,512,1,img);
if(d==1){
fwrite(mem,512,1,photo);
}else{
break;
}
fread(sign,4,1,img);
}while(!((sign[0] == 0xff) && (sign[1] == 0xd8) && (sign[2] == 0xff) && (sign[3] >= 0xe0) && (sign[3] <= 0xef)));
fclose(photo);
fseek(img,-4,SEEK_CUR);
if(d!=1) break;
}while(true);
fclose(img);
return 0;
}
I would greatly appreciate if someone could help me find what I am doing wrong in my code. Thank you!