I can see all the images from the card.raw file but Check50 keeps giving me the following when running it on Pset 4 Recover:

:) recover.c exists.
:) recover.c compiles.
:) handles lack of forensic image
:) recovers 000.jpg correctly
:( recovers middle images correctly
   recovered image does not match
:( recovers 015.jpg correctly
   recovered image does not match

I've been trying everything I can think of to make this work. I've looked at the questions and responses on this forum. I have also changed the logic in my program multiple times to try and figure out my mistake. Any clue as to how I what direction I might take would be greatly appreciated :)

Here's my code:

int main(int argc, char *argv[])
    if (argc != 2)
        fprintf(stderr, "Usage: ./recover image\n");
        return 1;

    char *inptcrd = argv[1];

    // open memory card file. Return 2 if it can't be read.
    FILE *memcard = fopen(inptcrd, "r");
    if (memcard == NULL)
        fprintf(stderr, "Could not open %s for reading.\n", inptcrd);
        return 2;

unsigned char cardbuffer[512];
//counter for filenames
int count = 0;
//create file name
FILE *outptjpg = NULL;
char filename[8];
//create a variable that is false until the first picture is found
bool has_image = false;

while (fread(cardbuffer, sizeof(512), 1, memcard))
   if (cardbuffer[0] == 0xff && cardbuffer[1] == 0xd8 && cardbuffer[2] == 0xff && cardbuffer[3] >= 0xe0 && cardbuffer[3] <= 0xef)
        has_image = true;

        if (outptjpg != NULL)
            //close file

        //account for zeros in filename. create the name based on the current count.
        sprintf(filename, "%03i.jpg", count);

        // open output file
        outptjpg = fopen(filename, "w");
        //add to count for next file name

    if (has_image)
        //write the contents of buffer to file
        fwrite(cardbuffer, sizeof(512), 1, outptjpg);



return 0;


1 Answer 1


I figured it out! So, I had gone through my code many times before asking for help but one of the only things that I had not thought to consider was taking a closer look at the arguments in fread:

while(fread(cardbuffer, sizeof(512), 1, memcard))

I made a change to one of the arguments on this line of code and now it works.

  • I forgot to mention that I made the same change to the argument in fwrite() that I had made in fread()
    – Seph
    Oct 11, 2017 at 20:56
  • What change did you make? Oct 31, 2018 at 13:41
  • Hi Mark, here are the changes I made: fread(cardbuffer, sizeof(cardbuffer), 1, memcard) and fwrite(cardbuffer, sizeof(cardbuffer), 1, memcard) I don't think I made any other changes although it's been awhile since I looked at this code :) Hope this works for you
    – Seph
    Nov 1, 2018 at 19:19
  • Oops, I made an error just now. That second line of code should be fwrite(cardbuffer, sizeof(cardbuffer), 1, outptjpg)
    – Seph
    Nov 1, 2018 at 19:29

You must log in to answer this question.

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