0

My recover program manages to recover 50 images from 000-049jpg however this images are incomplete. I have been struggling with getting my recover program to recover whole images. I have endlessly searched this site for an answer and tried several changes to no avail

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


typedef uint8_t  BYTE;

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

//remember file names
char *infile = argv[1];

//open file

FILE *file;

file = fopen(infile, "r");

if (file == NULL)
{
  printf("Error! Could not open the file!\n");
  return 1;
}

//declare image file

FILE *img;

img = NULL;

//declare read buffer



BYTE buf[512] = {0} ;

//declare name buffer

char filename[8];

//declare file counter

int counter = 0;

int fwrites = 0;

// while fread is true
while (fread(buf, 512, 1, file) == 1)
{  

    // if it is a jpeg
    if (buf[0] == 0xff && buf[1] == 0xd8 && buf[2] == 0xff && (buf[3] == 0xe0 || buf[3] == 0xe1))
     {  //close file if it has been opened
         if (img != NULL)
         {
             fclose(img);


         }

        //open new file

        sprintf(filename, "%03i.jpg", counter);

        img = fopen(filename, "w");

        if (img == NULL)

        {
          printf("Error! Could not create the image file!\n");
          return 1;
        }

         fwrite(buf, 512, 1, img);

         counter ++;

     } 
//write to file


if (img != NULL)
{
fwrite(buf, 512, 1, img);

fwrites ++;
}



}

fclose(img);


fclose(file);

printf("Writes: %i ", fwrites);

return 0;

}

I concluded that my program discovers and writes the first part of the jpegs correctly but cannot fill in the rest of the files as there are only partial images. Thus I feel like the error resides in this bit of code which is not executing

if (img != NULL)
{
 fwrite(buf, 512, 1, img);

 fwrites ++;
 }

So I used an int fwrites to check how many times the code runs and it always returns 23105. I am thus confused with how to fix my code.

Check50 returns :) recover.c exists :) recover.c compiles :) handles lack of forensic image :( recovers 000.jpg correctly :( recovers middle files correctly :( recovers last file correctly

my program recovers 50 pictures like this one

enter image description here

enter image description here

I am complete novice at programming and this is my first CS course so any and all help will be appreciated.

1 Answer 1

1

You write the first block of each jpg twice. Remove the first fwrite (which is my preferred option, as you now have only one fwrite and don't repeat yourself DRY), or make the block of the second fwrite an if else instead of if.

I'd add another if (img != NULL) to that final fclose(img), in case the card image does not contain a single jpg.

1
  • thank you so much. that sorted everything out. the fix was much simpler than i thought it would be
    – nadx97
    Commented Jul 25, 2017 at 14:18

You must log in to answer this question.

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