I can see all the images from the card.raw file but Check50 keeps giving me the following when running it on Pset 4 Recover:
:) recover.c exists.
:) recover.c compiles.
:) handles lack of forensic image
:) recovers 000.jpg correctly
:( recovers middle images correctly
recovered image does not match
:( recovers 015.jpg correctly
recovered image does not match
I've been trying everything I can think of to make this work. I've looked at the questions and responses on this forum. I have also changed the logic in my program multiple times to try and figure out my mistake. Any clue as to how I what direction I might take would be greatly appreciated :)
Here's my code:
int main(int argc, char *argv[])
{
if (argc != 2)
{
fprintf(stderr, "Usage: ./recover image\n");
return 1;
}
char *inptcrd = argv[1];
// open memory card file. Return 2 if it can't be read.
FILE *memcard = fopen(inptcrd, "r");
if (memcard == NULL)
{
fprintf(stderr, "Could not open %s for reading.\n", inptcrd);
return 2;
}
unsigned char cardbuffer[512];
//counter for filenames
int count = 0;
//create file name
FILE *outptjpg = NULL;
char filename[8];
//create a variable that is false until the first picture is found
bool has_image = false;
while (fread(cardbuffer, sizeof(512), 1, memcard))
{
if (cardbuffer[0] == 0xff && cardbuffer[1] == 0xd8 && cardbuffer[2] == 0xff && cardbuffer[3] >= 0xe0 && cardbuffer[3] <= 0xef)
{
has_image = true;
if (outptjpg != NULL)
{
//close file
fclose(outptjpg);
}
//account for zeros in filename. create the name based on the current count.
sprintf(filename, "%03i.jpg", count);
// open output file
outptjpg = fopen(filename, "w");
//add to count for next file name
count++;
}
if (has_image)
{
//write the contents of buffer to file
fwrite(cardbuffer, sizeof(512), 1, outptjpg);
}
}
fclose(outptjpg);
fclose(memcard);
return 0;
}