0

Any ideas where I'm going wrong?

I seem to load all of the recovery data into image "000.jpg", never climbing out until I reach the end of the file. However, I cannot understand why this is so.

Check50 is not amused at my attempts, regardless of what I do...

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

unsigned char box[512]; // monsieur buffer
char filename[16];
FILE *image = NULL;
int filecounter = 0; // set file counter to 0

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        printf("Usage: ./recover image");
        return 1;
    }

    FILE *f = fopen(argv[1], "r");  // Open memory card // FILE *f = fopen(filename, "r");

    if (!f)    // make sure there is something
    {
        return 1;   // if cannot be read/opened, return 1.
    }

    //Repeat untill I reach the end of the card....
    while (!feof(f))
    // read the file's first block into the box
    {
        fread(box, 512, 1, f);
        // alter the fourth byte, in case it starts with the right values, to join the previous 3 values as a jpeg signatre check
        //(box[3] & 0xf0) == 0xe0;   // if passes jpeg signature check...
        if (box[0] == 0xff && box[1] == 0xd8 && box[2] == 0xff && (box[3] & 0xf0) == 0xe0)
        {
            if (filecounter != 0)   // if this is the first pass it will skip this...
            {
                fclose(image);
                filecounter++;   // Up the counter to say that we have found a new jpeg
            }
            sprintf(filename, "%03i.jpg", filecounter);   // Create a new file a new file
            image = fopen(filename, "w");  // Open the new file
            fwrite(box, 512, 1, image);   // Write to the new file
        }

        // if no new JPEG header is found
        else
        {
            // unless image is still NULL, continue writing to image
            if (image != NULL)
            {
                fwrite(box, 512, 1, image);
            }
        }
    }
    fclose(image);
    fclose(f);
    return 0;
}
0

There are 2 errors in your code but both the errors are due to 1 data type "filecounter". Here the correct code.

 if (filecounter != 0)   // if this is the first pass it will skip this...
            {
                fclose(image);// Up the counter to say that we have found a new jpeg
            }
            sprintf(filename, "%03i.jpg", filecounter++);   // Create a new file a new file
            image = fopen(filename, "w");  // Open the new file
            fwrite(box, 512, 1, image);   // Write to the new file

The filecounter in the sprintf function was not incrementing, thus overwriting the data to one image. And filecounter in if loop was an unnecessary line conflicting with the issue of names of recovered images. Try yourself pal.

If this answers your question, please click on the check mark to accept. :)))))

| improve this answer | |
  • Thank you. Yes, more than two errors, it turns out, but I've made two types of errors, so to speak. The first type relates to my placing of filecounter; since the present format will never remain on file "000" indefinitely. The second is related to my ill-advised usage of eof, which I replaced with a simpler (and more sensible) logic statement. – RandomForestRanger Jun 15 at 6:58

You must log in to answer this question.

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