0

I am facing some troubles, only one blank image is generated , any help?

// Program to recover lost photos.
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#define FAT_BLOCK_SIZE 512

// Program starts here
int main(int argc, char *argv[])
{
    // Checking the commandline argument
    if (argc != 2){
        fprintf(stderr,"Usage: ./recover image\n");
        return 1;
    }
    char *file = argv[1];
    // Checking the validaty of the file
    FILE *ptr = fopen(file,"r");
    if (ptr == NULL){

        fprintf (stderr,"File not found!\n");
        return 2;
    }

    int imgCount = 0;
    bool imgC;
    uint8_t *imgBlock = malloc(FAT_BLOCK_SIZE);
    while (fread(imgBlock,512,1,ptr) != EOF)
    {

        fread (imgBlock, 512 , 1 , ptr);

        if (imgBlock[0] == 0xff && imgBlock[1] == 0xd8 &&
            imgBlock[2] == 0xff && (imgBlock[3] & 0xf0 ) == 0xe0)
            {
                     sprintf (file,"00%i.jpg",imgCount);
                     FILE* img = fopen("00imgCount.jpg","w");
                     fwrite(imgBlock,512,1,img);

                     imgCount++;

            }




    }


fclose(ptr);
free(imgBlock);
return 0;


}
1

The code for naming and opening the output file is wrong.

                 sprintf (file,"00%i.jpg",imgCount);
                 FILE* img = fopen("00imgCount.jpg","w");

The sprintf line is used to build the name of the output file and store it in a variable. Then, the fopen actually opens the file. Both of these lines have problems.

The sprintf call creates a string with two leading zeros followed by an int. When the file count gets to 10, the filename would be 0010.jpg. That won't work. But that's a moot point because of what follows.

The fopen doesn't even use the filename. It tries to create the file using a fixed string, specifically, 00imgCount.jpg. When you enclose the entire string in quotes like that, it doesn't use the value stored at the variable name given. It uses that variable name as actual text. You need to use a placeholder, like %i and put the var name after the string. (the sprintf line shows how to do that, same as any printf statement.)

That covers why there's only one file. It has the wrong name and is being recreated over and over. Once this is fixed, make sure that the data is being written out to the correct file pointer name.

Next, there's no code to write out data blocks without signatures after the first signature is found. As stated in the spec, once the first signature is found, all the data belongs to image files.

Finally, the program doesn't terminate. It goes into an infinite loop because it doesn't correctly detect for EOF. Hint: read the man page for the fread function, particularly return values. Also, what is the numeric value of EOF?

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

2
  • surely this answer helped me and i observed some mistakes , thx alot – Yosef Hesham Sep 28 '19 at 20:52
  • Glad to hear. If your question was answered, please click on the check mark to accept. Otherwise your question will sit in the unanswered question pool forever. thanks. – Cliff B Sep 28 '19 at 21:08

You must log in to answer this question.

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