I wrote recover.c and I am for sure not expecting it to work perfectly at once but it is just not doing anything! When I enter ./recover nothing and I mean literally nothing happens... Is ./recover not enough? Do I have to enter further command line arguments? I updated my workspace. The code compiles.. I don´t know where the problem is, as I in general don´t feel so bad about this code. Maybe I am just missing the forest for teh trees... Maybe anyone has an idea? Thank you! int main(int argc, char* argv[]) { //open file FILE* card = fopen("card.raw", "r"); if (card == NULL) { printf("could not open%s\n", "card.raw"); return 1; }

//declare image file
FILE* image;
int n = 0;

// string to hold name
char* name = malloc((7* sizeof(char))+1);

//create buffer
BYTE buffer[512];

//start reading
while(fread(buffer, sizeof(buffer),1, card) == 1)
   //go back
   fseek(card, -512, SEEK_CUR);

   //read till find new jpg
   fread(buffer, sizeof(BYTE), 512, card);

    //if there is a jpg
    if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && buffer[3] == 0xe0 && buffer[4] == 0xe1)
        //close previous file
        if(n !=0)

        //create filename
        sprintf(name, "%3d.jpg", n);

        //open new file
        image = fopen(name, "a");
        if(image == NULL)
            printf("could not open %s\n", name);
            return 2;

        //write into the outfile
        fwrite(buffer, sizeof(BYTE), 512, image);
        //continue writing to old file , if there is no new jpg
        if(n !=0)
        fwrite(buffer, sizeof(BYTE), 512, image);

It looks as if you have a problem with your signature test:

if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff 
             && buffer[3] == 0xe0 && buffer[4] == 0xe1)

The file signature is only 4 bytes long, yet the code checks 5 bytes. Making the problem worse, it is supposed to check for 16 different possible signatures, not just one. Simply put, it never finds a signature, all it is doing is reading through the input file and quitting. You need to rework the signature check as specified in the pset instructions (and not in Zamyla's video that says there are only two signatures. It's an old video.)

As a side note, the code reads in 512 bytes in the while setup, then fseeks back to the beginning of that block, then reads in the same block again. Why do you feel this is necessary? While it will work, why do you think it is necessary? All that really does is double the program's run time. (File reads and writes dominate processing time because the i/o time for a traditional hard drive can be as much as 1000 times longer than any memory access. With new SSD drives, the differential is substantially less, but still has a notable impact.)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

  • Thank you. I chnaged my signature test and it recovers all the images now. I tried a lot of things because it never did anything. So I haven´t really spared a thought for the run time yet... This version just felt save. But I can try to optimize the run time. What would you suggest? – user10882 Jun 6 '16 at 9:34

You must log in to answer this question.

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