I finished resize after spending days on it and took a week break, today I read what recover is and after reading it I am having that feeling as to what to do where to start from? Have spend like ours reading the specs and thinking about it still could not understand. Having that feel that i havent learnt any thing and that I dont know C and etc and its scaring me, I really wanted to complete this course and get a certificate but i am getting the feeling this is where I might quit.... Plz help, any sort of help would be appreciated. Anyone else found recover this much tough?

#include <stdio.h>



int main(int argc, char* argv[]) { // open card file FILE* inptr = fopen("card.raw", "r");

// check for errors
if (inptr == NULL)
    printf ("could not open card. \n");
    return 1;

// declaring a block of 8 bits of Type BYTE and having 512 blocks
typedef uint8_t BYTE;
BYTE block[512];

int counter = 0;
while (true)
    // checking for if end of data on card has reached.
    fread(&block, 512, 1, inptr);

    // checking for headers of JPEGs
    if (block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff && (block[3] == 0xe0 || block[3] == 0xe1))
        char name[7];
        sprintf(name, "00%i.jpg", counter);
        FILE* picture = fopen(name, "w");
        fwrite(&block , 512, 1, picture);

        // going on to next block
        fread(&block, 512, 1, inptr);

        //loop for keep on reading blocks by blocks after the header
        while (block[0] != 0xff && block[1] != 0xd8 && block[2] != 0xff && (block[3] != 0xe0 || block[3] != 0xe1))
            fwrite(&block, 512, 1, picture);
            fread(&block, 512, 1, inptr);




1 Answer 1


calm down! is really easier than you think!

so the basic idea is that we have a file, card.raw, which contains images in JPG format, exactly 16 of them. each of these 16 JPGs begins with a special signature — a set of unique bytes (exactly 4 bytes) that identify the beginning of a JPG.

there is nothing to mark the end of each JPG, but the JPGs follow one another in card.raw, so the beginning of a JPG means the end of a previous one.

there are two special cases here and these are the very first JPG (which doesn't have a previous JPG) and the very last JPG (which doesn't have a following JPG), so we are probably gonna need to handle those cases in special ways.

we have also been told per the specs that card.raw consists of 512-byte blocks.

what we basically want to do is

open card.raw to read
read the data in card.raw block by block
if the first 4 bytes of the recently read block mark the beginning of a JPG
     close the currently opened JPG (if any)
     open a new JPG

 write the recently read block to the currently opened JPG (if any)

the pseudocode above is intentionally written not to be directly ported to actual C code in order to give you some room to think! let me know if you need more hints!

  • hey thanks for the help man really meant a lot, I wrote the code that i have added in the edit portion, but this is not working I am only getting a 000.jpg file created which seems ok and just another file named as 001.jpg which is not correct. Can you help me more plz Dec 11, 2015 at 19:43

You must log in to answer this question.

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