My code compiles and recovers all images, but check50 keeps throwing me an error:

:) 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 https://sandbox.cs50.net/checks/6ec4618ac09d431e8c0d023b7d0ea50a

I've read a few of the similar problems people are having but the solutions did not seem to apply to my code. Can anyone help me figure this out?

Also, when I run ./recover, a new file called ˆA appears on the top of the file tree. It has this inside: bit.ly/18gECvy, which is a link to a German page about hair loss. Now, I am at a loss here.

Please consider: inptr points to the raw file, outptr points to the output file, title is a char size [8], buffer is a uint8_t size [512] and counter is initialized to 0.

Where am I going wrong?

while (!feof(inptr))
    if (fread(&buffer, sizeof(buffer), 1, inptr) == 1)
        if ((buffer[0] == 0xff) && (buffer[1] == 0xd8) && (buffer[2] == 0xff) && ((buffer[3] == 0xe0) || (buffer[3] == 0xe1))) 
          sprintf(title, "%.3d.jpg", counter);
        outptr = fopen(title, "a");
        fwrite(&buffer, sizeof(buffer), 1, outptr);   

Close input file and return 0.


That ˆA file appears because the first bytes of card.raw are garbage values (aside from that easter egg link you've found).

The pictures only start to appear after a couple of blocks.

Your code reads and recovers the entire content of card.raw, but you're supposed to recover only the jpg files inside it.

Maybe you should add a condition before your call to fopen() and fwrite() to make sure you only write to the outfile if you have already found the first jpg?

  • 1
    I did it. More than one reason for counter, I see. But I had to move my fread() back to the loop condition, otherwise it wouldn't work. Check50 is all smiles. Thank you again, @Yuri :) Nov 17 '16 at 17:17
  • Great! Other challenges lie ahead! Good luck! Nov 17 '16 at 17:22

You must log in to answer this question.

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