0

I am trying to recover the deleted files in card.raw (argv[1]) I managed to open the jpeg files in order(001 to 049) but the problem is, when I click on jpg files, the following message show "invalid or unsupported image format" with blank images.

And I am still looking for the problem in my code.

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



int main(int argc, char *argv[])
{
// ensure proper usage 2arguments and .raw file
if (argc != 2)
{
    fprintf(stderr, "Usage: ./recover filename.raw\n");
    return 1;
}

// open input file 
FILE *memCard = fopen(argv[1], "r");

if (memCard == NULL)
{
    fprintf(stderr, "Could not open %s.\n", argv[1]);
    return 2;
}

FILE *img=NULL;
char filename[8];
int count=0;
uint8_t buffer[512];

// if there is a block with 512bytes long continue the process
while( fread(buffer, 512, 1, memCard) == 1){

    //Start of new jpeg file
    if( buffer[0] == 0xff &&
        buffer[1] == 0xd8 &&
        buffer[2] == 0xff &&
        (buffer[3] & 0xf0) == 0xe0){

            //close current image file if there is any.
            if(img != NULL){
                fclose(img);
            }

            sprintf(filename, "%03i.jpg", count);
            img = fopen(filename, "w");
            fwrite(memCard, 512, 1, img);

            count++;


    }else{

        // jpg file already open, continue writing
        if(img != NULL){
            fwrite(memCard, 512, 1, img);                    
        }
    }
}




// close infile
fclose(memCard);

// close outfile
fclose(img);

// success
return 0;

}

  • Maybe 1) while (fread(buffer, BLOCK_SIZE, 1, img)) 2) char filename[8]; inside the while{} – Elizabeth Bogdanova Apr 13 '17 at 10:40
  • thanks for the suggestion :) – Jung-min OK Apr 13 '17 at 16:51
1

Looks like a typo in the fwrite statements. You want to write the block that was just read into buffer (not memCard).

| improve this answer | |
  • oh yeah, you are right, thanks a lot :D it works! – Jung-min OK Apr 13 '17 at 16:51

You must log in to answer this question.

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