0

Initially, I thought my code was only recovering 2 images from the card.raw file but after looking at the code with debug - I think it is recovering the 50 files but it overwrites 001.jpeg many times. When looking at program in debug, it seems like when a jpeg is found it steps into the if statement to create a file, creates the first file (000.jpeg) and increments file_number, then finds the next file and writes it (001.jpeg) but every time it runs the sprintf function to create the file name, it resets file_number to 0. My question is why? I suspect it has something to do with the scope of the variable...?

    // define a buffer array
    unsigned char buffer[512];

    // filename incrementing variable
    int file_number = 0;

    // create an array for filenames
    char file_name[8];

    while(fread(buffer, sizeof(buffer), 1, inptr) == 1)
    {

        // check buffer to see if it is a start of a jpeg
        if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        {
            // close the file if it isn't the first jpeg
            if(file_number > 0)
            {
                fclose(jpeg_file);
            }

            // create a name for the new file with format xxx.jpeg
            sprintf(file_name, "%03i.jpeg", file_number);

            // open a new file for writing jpegs
            jpeg_file = fopen(file_name, "w");
            if (jpeg_file == NULL)
            {
                fprintf(stderr, "Could not open %s.\n", file_name);
                return 2;
            }

            // write the buffer into the file
            fwrite(buffer, sizeof(buffer), 1, jpeg_file);

            // increment the file counter
            file_number++;

        }

        // write the buffer into the file if a jpeg have already been found
        else if(file_number > 0)
        {
            // write the buffer into the file
            fwrite(buffer, sizeof(buffer), 1, jpeg_file);
        }

    }
0

Oh boy... I figured it out after looking over other posts...

The char file_name[8]; includes enough characters for xxx.jpeg, not xxx.jpeg\n. I changed the naming of the files to sprintf(file_name, "%03i.jpg", file_number); and everything worked! Thanks to anyone who was looking through the code for me!

4
  • 1
    Well, close. The last byte will contain the end of string marker, \0, not the line feed char, \n Happy coding! ;-) – Cliff B Aug 26 '18 at 21:32
  • Now, have you figured out the file name problem? :-O – Cliff B Aug 26 '18 at 22:29
  • Yes, I did mean \0 and not \n - good catch. I think I have the name figured out, when I ran the updated code it create the correct number of jpg's with the correct name. They were even legit files of images! Thanks! – covrebo Aug 27 '18 at 1:39
  • 1
    Keep in mind that char file_name[8]; would be perfectly fine if you named your files per the spec: "000.jpg" (you won't pass check50 if you name them "000.jpeg") – curiouskiwi Aug 27 '18 at 8:22

You must log in to answer this question.

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