0

So, I have finished recover.c (2016 edition) and it recovers all 50 of the jpg-s, and labels tham from 000 to 049.

I have looked at every jpg, and they seem intact, all of them (I opened each of them individually - took forever with the IDE). Yet when I pass my code to check50 I get:

:) recover.c exists

:) recover.c compiles

:) recovers 000.jpg correctly

:( recovers middle files correctly

:( recovers last file correctly

I have no idea what may be wrong. I'm currently trying valgrind to check for leaks (doubt I have any or it counts for check50).

I'll post here part of my code. If anyone can shed some light on what may be wrong, that would be superb.

    while (true)
{
    //checks if end of the card is reached
    if (fread(&buffer, sizeof(BYTE), 512, inptr) != 512)
    {
        fclose(inptr);
        break;
    }

    else
    {
        // finds the start of jpgs
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && buffer[3] <= 0xe0 && buffer[3] >= 0xe0)
        {
            found = true;

            // creates a jpg file for me to write into
            sprintf(title, "%03d.jpg", piccount);

            // opens file for writing
            FILE* image = fopen(title, "w");

            // writes the first 512 bytes to the outfile
            fwrite(&buffer, sizeof(BYTE), 512, image);
            fclose(image);
            piccount++;
        }

        else if (found == true)
        {
            // appends additional bytes til new jpg is found
            FILE* image = fopen(title, "a");
            fwrite(&buffer, sizeof(BYTE), 512, image);
            fclose(image);
        }
    }
}
1
  • Okay just noticed it also says: " \ expected 015.jpg to exist " But 015.jpg does exist... Also: Just ran valgrind, it said: HEAP SUMMARY: ==47405== in use at exit: 0 bytes in 0 blocks ==47405== total heap usage: 33,388 allocs, 33,388 frees, 18,964,384 bytes allocated ==47405== ==47405== All heap blocks were freed -- no leaks are possible ==47405== ==47405== For counts of detected and suppressed errors, rerun with: -v ==47405== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
    – Bubi
    Commented Feb 3, 2016 at 17:32

1 Answer 1

2

It's interesting that you get 50 files, but something is amiss. Look at your test for signatures:

if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && buffer[3] <= 0xe0 && buffer[3] >= 0xe0)

The problem revolves around the testing of buffer[3]. You are checking that it is both <= and >= 0xe0, meaning it must equal 0xe0. From the pset instructions:

The fourth byte, meanwhile, is either 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, of 0xef. Put another way, the fourth byte’s first four bits are 1110.

The signature test needs to be rewritten to satisfy the requirement. When I corrected it and plugged your code into mine, it worked fine.

Also, be sure that you are coding to the current year's assignment. It has changed from last year. In fact, the signature check is probably the biggest change.

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

1
  • Wow! Keen eyes you got! Yes, that was it, thank you so much, it passes check50 now. Interesting that it recovered them in the first place (or hasn't recovered some junk in the process). I really appreciate the help!
    – Bubi
    Commented Feb 3, 2016 at 18:14

You must log in to answer this question.

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