Trying to find the problem i could track it to the else statement where ""printf("Could not open file because NULL.\n");"" is located. The files are not read at all or saved at all and i am not sure why becuase it compiles.

:) recover.c exists.

:) recover.c compiles.

:) handles lack of forensic image

:( recovers 000.jpg correctly 000.jpg not found

:( recovers middle images correctly 001.jpg not found

:( recovers 049.jpg correctly 049.jpg not found

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

int main(int argc, char *argv[])

//checks if there is an input
if (argc !=2)
    printf("./recover Usage: ./recover image \n");
    return 1;
} else


    char *filename = argv[1];
    FILE *input = fopen(filename, "r");

    if (strcmp(filename, "card.raw") != 0)
        printf("Unable to open: %s\n", filename);
        return 2;
    } else

        //Create buffer

        uint8_t buffer[512];

        //Create pointer for the buffer
        FILE *output = NULL;

        // create 8 bytes file

        char files[8];

        //Files counters

        int jpeg_counter = 0;


        while (fread(buffer, sizeof(buffer), 1, input))

            //check 4 first bytes of file to see if they contain the jpg signature

            if ((buffer[0] == 0xff) && (buffer[1] == 0xd8) && (buffer[2] == 0xff) && ((buffer[3] & 0xf0) == 0xe0))
                if (output != NULL) {
                    // sprintf(char *str, const char *format, ...) and "03i" means 3 digits in format 001, 002...
                    sprintf(files, "%03i.jpg", jpeg_counter);
                    //use created pointer to save jpeg files from input folder card.raw
                    output = fopen(filename, "w");
                    //add jpeg to the new buffer using ouput pointer
                    fwrite(buffer, sizeof(buffer), 1, output);
                    // update counter
                }   else //no more files to read(end of folder array).
                    printf("Could not open file because NULL.\n");
                    return 0;




    return 0;


} }

1 Answer 1


Why is the code comparing the input filename given as a parameter to card.raw?

if (strcmp(filename, "card.raw") != 0)

The input filename might as well be hardcoded. It totally defeats the purpose of having a filename as an input parameter.

The code should only be checking that the input file was actually opened.

You must log in to answer this question.

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