0

I have been working on pset4 recover, but it is bringing one error how can i rectify it? Here is my code

    FILE* card_ptr = fopen("card.raw", "r");
    if (card_ptr == NULL)
    {
        printf("Could not open card.raw.\n");
        return 2;
    }

    // create buffer
    unsigned char buffer[512];

    // filename counter
    int filecount = 0;

    FILE* picture = NULL;

    // check if we've found a jpeg yet or not
    int new_jpg_found = 0;

    // go through cardfile until there aren't any blocks left
    while (fread(buffer, 512, 1, card_ptr) == 1)
    {
        // read first 4 bytes of buffer and see if jpg signature using bitwise on last byte
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xe0) == 0xe0)
        {
            if (new_jpg_found == 1)
            {
                // We found the start of a new pic so close out current picture
                fclose(picture);
            }
            else
            {
                // jpg discovered and now we have the green light to write
                new_jpg_found = 1;

            }

            char filename[8];
            sprintf(filename, "%03d.jpg", filecount++);
            picture = fopen(filename, "w");
            if(picture == NULL)
            return 1;
        }

        if (new_jpg_found == 1)
        {
            // write 512 bytes to file once we start finding jpgs
            fwrite(&buffer, 512, 1, picture);
        }

    }

    // close files
    fclose(card_ptr);
    fclose(picture);

    return 0;
}
0

Please also tell us the error, whether it is a compile error, a runtime error, wrong output, or check50 tests failing.

So I'm just looking for obvious things.

First issue: Per specification, the input file name is in argv[1].

Your program should accept exactly one command-line argument, the name of a forensic image from which to recover JPEGs.

Second issue: If anything goes wrong, return 1, not 2.

I would also give the last fclose(picture); an if, just in case you never open an output file.

| improve this answer | |

You must log in to answer this question.

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