1

I finally got recover.c to compile and work. Everything looks right: files are padded with zeroes, all 50 jpgs are recovered and readable. But when I run check50, it fails the check of recovering the last file correctly. Opening the jpg, it looks completely fine.

I looked through the forums at people who had posted with similar problems, and I attempted multiple fixes based on those responses. I've moved the eof check to the beginning and end of the loop. I tried using if(fread(&buffer, BLOCK, 1, memcard) == 1) as my outside loop, but that makes all the jpgs unreadable. Still no luck, what am I doing wrong?

Code for main below, BLOCK is #defined as 512.

//open memory card file
FILE* memcard = fopen("card.raw", "r");

//initialize count of jpgs for file naming
int imgNum = 0;

//create a buffer made up of 512 8-bit bytes
typedef uint8_t BYTE;
BYTE buffer[BLOCK];

//declare string to put image name in
char title[8];

//initialize file to put images in
FILE* img = NULL;

//loop over the entire card
do
{
    //read a block into the buffer
    fread(&buffer, BLOCK, 1, memcard);

    //check if block starts with jpg signature
    if (buffer[0] == 0xff && buffer [1] == 0xd8 && buffer[2] == 0xff && (buffer[3] == 0xe0 || buffer[3] == 0xe1))
    {
        //if jpg file is already open, close it
        if(img != NULL)
            fclose(img);

        //create new file and increase jpg count
        sprintf(title, "%03d.jpg", imgNum);
        img = fopen(title, "w");
        imgNum++;

        //write block to file
        fwrite(&buffer, BLOCK, 1, img);
    }

    //if block doesn't start w/signature and file is already open, write next block
    else if (img != NULL)
        fwrite(&buffer, BLOCK, 1, img);

    //close last jpg to avoid corruption and pass check50
    if(feof(memcard))
    {
        fclose(img);
        return 0;
    }    
}
while(!feof(memcard));

//close memory card file
fclose(memcard);

Thanks in advance!

1

Since it's the last jpg that gives trouble, EOF handling is definitely the prime suspect. You can rewatch the FILE I/O short starting around 7:30 to refresh your memory on why using feof to detect end-of-file is fraught. The function writes the "feof block" here:

else if (img != NULL)
        fwrite(&buffer, BLOCK, 1, img);

before the feof(memcard) test.

The image "looks" ok because the eye cannot detect the "extra pixels" at the end of the file unless you zoom in really really really close.

You must log in to answer this question.

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