0

During recover of pset4, I manage to make all the files are made but when opening, an error pops up.

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

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

typedef uint8_t  BYTE;

int main(void)
{
    // import file
    FILE* file = fopen("card.raw","r");

    char* title = malloc(3 * sizeof(char));
    int count = 0;
    BYTE buffer[512];
    FILE* img;

    while(1)
    {  
        // load bytes
        fread(buffer, sizeof(BYTE)*512 , 1, file);

        // look for beginning
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] == 0xe0 || buffer[3] == 0xe1))
        {
            if (count != 0)
            {
                fclose(img);
            }
            if (count<10)
            {
                sprintf(title, "00%i.jpg" , count);
            }
            else if(count < 100)
            {
                sprintf(title, "0%i.jpg" , count);
            }
            else
            {
                sprintf(title, "%i.jpg" , count);
            }
            img = fopen(title, "a");
            count++;
        }
        if (count > 0)
        {
            if (fread(buffer , sizeof(BYTE) * 512 , 1 , file) !=0)
            {
                // do stuff with the data
                fwrite(buffer , sizeof(BYTE) *512 , 1 , img);
            }
            else
            {
                // the program is over and return 0
                free(title);
                return 0;
        }
        }
    }


}

Thanks for your help!

1

Are you sure this code is recovering all 50 jpgs? It seems to me like you're missing half the files and only recovering 25 pics, because you're calling fread here:

fread(buffer, sizeof(BYTE)*512 , 1, file);

And then here again, before writing it:

if (fread(buffer , sizeof(BYTE) * 512 , 1 , file) !=0)

Also, you're allocating only 3 chars for "title", but the title needs more chars than that... count everything inside the double quotes here and see what I mean (don't forget the null terminator): "000.jpg"

And I'm not saying your code is completely correct besides these mistakes, but these are the biggest ones.

You must log in to answer this question.

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