My recover program currently successfully returns a single image "001.jpg" which looks good when I open the file. It also returns a second image "002.jpg" which prompts an error for an Unsupported or Invalid Format.

int current_pic = 0;
char filename[8];
sprintf(filename, "%03i.jpg", file_count);
FILE* img = fopen(filename, "w");
//printf("%i", file_count);
fread(buffer, 512, 1, read_file);
    //If first three file BYTES indicate png, create file, write first block to file..
    if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        current_pic = 1;
        //printf("Current Pic: %i \n", current_pic);
        file_count ++;
        //printf("Filename: %i - - %s \n", file_count, filename);
            //Write the buffer into the file.

        fwrite(buffer, 512, 1, img);
        while (fread(buffer, 512, 1, read_file)==1)
            fwrite(buffer, 512, 1, img);
            buffer[0] = '\0';

} while (file_count <51);

The above runs well for a single JPG, but then does not move to the next one successfully (though it seems to get to the point of creating the second file).

I was thinking that it gets stuck in the while loop, so I tried pulling out the while loop, which then allowed me to create all 50 files, but without content in them.

Should I be thinking about not using a While at all, and only IF conditionals?

Thanks much for the help.

1 Answer 1


A loop is definitely the way to go, not if statements.

Now, have you looked at the size of the file(s) produced? I suspect that the first file created is much larger than it should be - a significant percentage of the input file.

Let's look at the code and see what it is really doing. The code searches for the first signature. Once found, it opens an output file and starts writing to it. The big problem is that once it starts writing, the inner while loop starts copying 512 byte blocks to the output file until it reaches the end of the input file. It isn't checking for any signatures! Yes, the first output file appears to be correct, but the header data at the start of the file tells the O/S to only display the first image's data. The remaining data in the file is still there, but is treated as the fill data at the end of the file.

So, the code needs to be modified to search for new signatures at the beginning of each data block to identify the start of the next jpg file.

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

  • I think this makes sense. I'm trying to use "break" now during a loop, which feels like is getting me on the right track. I also looked at the size of the files - looks like they're about 50mb.
    – Jake
    Jun 29, 2018 at 16:58
  • Fixed! Thanks for the help. Ended up just using one while loop, and one fread statement. For a while I was getting 17 or 25 pictures, not 50, and I think it was because I was using fread multiple times without always writing to a file.
    – Jake
    Jun 29, 2018 at 20:02

You must log in to answer this question.

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