0

My implementation of Recover produces 50 JPGs but does not pass check50. I am stumped. Any help would be greatly appreciated. message from check50

#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#define BLOCKSIZE 512
typedef __uint8_t BYTE;

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

    // remember filenames
    char *infile = argv[1];

    // open input file
    FILE *inputFile = fopen(infile, "r");
    if (inputFile == NULL)
    {
        fprintf(stderr, "Could not open %s.\n", infile);
        return 2;
    }

    // setting the cursor to the beginning of the file

    // reading from the file
    unsigned char buffer[BLOCKSIZE];
    int numOfJPGFound = 0;
    char name[50];
    FILE *img = NULL;
    while (!feof(inputFile))
    {
        fread(buffer, 1, BLOCKSIZE, inputFile);

        // checking the first four bytes of the buffer for the signature bytes of a JPEG
        if (buffer[0] == 0xff &&
            buffer[1] == 0xd8 &&
            buffer[2] == 0xff &&
            buffer[3] >= 0xe0 &&
            buffer[3] <= 0xef)
        {
            if (numOfJPGFound > 0)
            {
                // close the previously opened file
                fclose(img);
            }
            numOfJPGFound++;
            // print the filename
            sprintf(name, "%03i.jpg", numOfJPGFound);
            // create the new file
            img = fopen(name, "w");
            // write the block to the file
            fwrite(buffer, BLOCKSIZE, 1, img);
        }
        // if there is an opened file and the program hasn't reached a new JPG yet while reading
        else if (numOfJPGFound > 0)
        {
            fwrite(buffer, BLOCKSIZE, 1, img);
        }
    }
    fclose(img);
    fclose(inputFile);
}
1

I found the answer. When printing the names of the files, I started at 001.jpg instead of 000.jpg. I fixed this by changing this line:

sprintf(name, "%03i.jpg", numOfJPGFound);

to this:

sprintf(name, "%03i.jpg", numOfJPGFound -1);
1

I was having this issue as well. I made a different mistake so my solution was different from yours but came across your question in my research. I noticed that you incremented the filename before you printed it.

numOfJPGFound++;
// print the filename
sprintf(name, "%03i.jpg", numOfJPGFound);

If you move numOfJPGFound++ to after the sprintf statement you won't have to -1 from numOfJPGFound

You must log in to answer this question.

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