0

So my recover program recovers all pictures fine to me, and passes all tests in check50, apart from the last one: recovers last file correctly...

Check50 link: https://sandbox.cs50.net/checks/be03b4e2c7ed48769363407fa5d13de0

My code:

/**
 * recover.c
 *
 * Computer Science 50
 * Problem Set 4
 *
 * Recovers JPEGs from a forensic image.
 */
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdint.h>


int main(int argc, char* argv[])
{
    // TODO
    typedef uint8_t BYTE;
    // Opening the .raw file

    FILE* card = fopen("card.raw", "r");
    FILE* out_fp = NULL;
    char jpgFileName[8];

    if (card == NULL) {
        printf("File doesn't exist");
        return 1;
    }
    BYTE buffer[512];
    //buffer = malloc(512);
    int jpgNumber = 0;

    // While not EOF
    while (feof(card) == 0) {

        fread(&buffer, 512, 1, card);
        // Find the beginning of the jpg file
        if (
            buffer[0] == 0xff &&
            buffer[1] == 0xd8 &&
            buffer[2] == 0xff &&
            (buffer[3] == 0xe0 || buffer[3] == 0xe1 || buffer[3] == 0xe2 ||  buffer[3] == 0xe3 || buffer[3] == 0xe4 || buffer[3] == 0xe5 || buffer[3] == 0xe6 || buffer[3] == 0xe7 || buffer[3] == 0xe8 || buffer[3] == 0xe9 || buffer[3] == 0xea || buffer[3] == 0xeb || buffer[3] == 0xec || buffer[3] == 0xed || buffer[3] == 0xee || buffer[3] == 0xef)
            ) 
            {

            if (jpgNumber != 0) {
                fclose(out_fp);
            }

            // Open a new jpg


            sprintf(jpgFileName, "%03d.jpg", jpgNumber);
            out_fp = fopen(jpgFileName, "w");

            jpgNumber++;
            }


            if (jpgNumber != 0) {
                fwrite(&buffer, 512, 1, out_fp);
            }


    }
    // Close remaining files
    fclose(out_fp);
    fclose(card);

}

Thank you!

7

Look at the sequencing of what the code does. The fread() call reads 512 bytes into the buffer, processes whatever was read, and then circles back to the top of the while loop to check for the EOF. That means that it's going to process beyond the end of the input file and corrupt the last output file. The presence of the EOF condition has to be checked BEFORE processing the data, not after.

Maybe it would be a good idea to look at the description of fread() and see how it detects EOF and if there's a way to use it.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

0

simply write the data from the previous iteration reading

so you will ignore all the data till you hit a jpg file then you start writing on the next iteration till you hit the end of file but the last iteration data (the incorrect one) won't be written

while (feof(card) == 0) {
    if (jpgNumber != 0) {
        fwrite(&buffer, 512, 1, out_fp);
    }
    fread(&buffer, 512, 1, card);
    // Find the beginning of the jpg file
    if (
        buffer[0] == 0xff &&
        buffer[1] == 0xd8 &&
        buffer[2] == 0xff &&
        (buffer[3] == 0xe0 || buffer[3] == 0xe1 || buffer[3] == 0xe2 ||  buffer[3] == 0xe3 || buffer[3] == 0xe4 || buffer[3] == 0xe5 || buffer[3] == 0xe6 || buffer[3] == 0xe7 || buffer[3] == 0xe8 || buffer[3] == 0xe9 || buffer[3] == 0xea || buffer[3] == 0xeb || buffer[3] == 0xec || buffer[3] == 0xed || buffer[3] == 0xee || buffer[3] == 0xef)
        ) 
        {
        if (jpgNumber != 0) {
            fclose(out_fp);
        }
        // Open a new jpg
        sprintf(jpgFileName, "%03d.jpg", jpgNumber);
        out_fp = fopen(jpgFileName, "w");
        jpgNumber++;
        }
}

You must log in to answer this question.

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