0

All 50 valid images are created, but the program did not pass Check50.

This is my updated code:

/** * recover.c * * Computer Science 50 * Problem Set 4 * * Recovers JPEGs from a forensic image. */

include

include

int main(void) { // open CF card FILE* CF_card = fopen("card.raw", "r"); if (CF_card == NULL) { fclose(CF_card); printf("Could not open %s.\n", "card.raw"); return 1; }

// define a counter that counts the number of JPEGs found, a buffer, a character that denotes a filename, and a global variable for the output file
int JPEG_counter = 0;
uint8_t buffer[512];
char filename[8];
FILE* img = NULL;

// read CF card
while(fread(buffer, sizeof(buffer), 1, CF_card) == 1)
{

    // JPEG signature hunt
    if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] >= 0xe0 && buffer[3] <= 0xef))
    {
        if (img != NULL)
        {
            fclose(img);
        }

        // JPEG found, create new file
        sprintf(filename, "%03d.jpg", JPEG_counter);

        // open newly created file
        img = fopen(filename, "w");

        // keep track of JPEG found
        JPEG_counter++;

        // write to newly created file
        fwrite(buffer, sizeof(buffer), 1, img);
    }

    // continue to write to the same file until the next JPEG is found
    else if (JPEG_counter > 0)
    {
         fwrite(buffer, sizeof(buffer), 1, img);
    }
    else
    {
        fread(buffer, sizeof(buffer), 1, CF_card);
    }

}
// close the file
fclose(CF_card);
return 0;

}

This is the Check50 report:

~/workspace/pset4/jpg/ $ check50 2015.fall.pset4.recover recover.c :) recover.c exists :) recover.c compiles :( recovers 000.jpg correctly \ expected 000.jpg to exist :( recovers middle files correctly \ expected 001.jpg to exist :( recovers last file correctly \ expected 015.jpg to exist https://sandbox.cs50.net/checks/096138a2dccf48b6af8e2311acacced8

0

Why did you add an fread() call at the end of the while loop? This effectively puts two fread() statements back to back. It also throws away every block of data that it reads! And, since check50 uses a different data file, it happens that it throws away every signature block, so no files are created. oops!

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

| improve this answer | |
  • If that fread() call at the end of the while loop is in fact irrelevant, how does the program recognize that when the block it reads is neither the beginning of a "newly found" JPEG nor part of a "previously discovered" JPEG, it skips over it and start reading and checking the next block? – Alex Chan Oct 11 '16 at 21:37

You must log in to answer this question.

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