0

I have run my program several times and it outputs all 50 images and all of them open correctly and look fine (000 to 049).
But the check50 says that:
:) recover.c exists
:) recover.c compiles
:) handles lack of forensic image
:) recovers 000.jpg correctly
:( recovers middle files correctly
:( recovers last file correctly

I am not able to figure out the problem with my code and cannot understand why the code fails at middle files and the last file, even being viewable.

Thanks,

Here's my code-

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdint.h>

typedef uint8_t  BYTE;
int main(int argc,char* argv[])
{
BYTE a[3], byte[512];
char str[8];
int d,num=0,c;
if(argc!=2)
{
    fprintf(stderr, "Usage: ./recover image\n");
    return 1;
}
char* image=argv[1];
FILE* file=fopen(image,"r");
if(file==NULL)
{
    fprintf(stderr, "Could not open %s.\n", image);
    return 2;
}
a[0]=0xff;
a[1]=0xd8;
a[2]=0xff;
FILE *fp=NULL;
while(!feof(file))
{
    d=0;
    c=fread(&byte,sizeof(byte),1,file);
    for(int i=0;i<3;i++)
    {
        if(a[i]==byte[i])
        d++;
    }
    if(byte[3]>=0xe0 && byte[3]<=0xef)
    d++;
    if(d==4)
    {
        sprintf(str,"%.3d.jpg",num);
        if(num>0)
        fclose(fp);
        fp=fopen(str,"w");
        fwrite(&byte,sizeof(byte),1,fp);
        num++;
    }
    else if(num==0)
    continue;
    else if(c==1)
    fwrite(&byte,sizeof(byte),1,fp);
}
fclose(file);
return 0;
}
2
  • First I zeroed in on sizeof(byte), but that's fine. Then I zeroed in on while(!feof(file)) but that's handled. Then I zeroed in on lack of {} around code blocks, but I'm easily confused. Finally I ran it and submitted it to check50. It passed. One conclusion is: this is not the code you submitted to check50. Apr 11 '17 at 12:18
  • Actually you were right, I had two files with different versions of recover and the above version did not have its name as recover, while I was just trying check50 which worked for file with its name as recover. I changed the name and now it passes check50. Thank you.
    – T_K
    Apr 11 '17 at 17:56
1

The above code is right, it was just that I saved it with name other than recover, while check50 ran on recover.c (which was faulty). I changed the name and now it works perfectly.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .