I'm not sure if check50 is working correctly? When i run my code, i recover all 50 jpg and i can view them. This is what i get when i run check50 2016.recover recover.c
:) recover.c exists
:) recover.c compiles
:) handles lack of forensic image
:) recovers 000.jpg correctly
:) recovers middle files correctly
:( recovers last file correctly
https://sandbox.cs50.net/checks/0e458fffc22f415aac1db80f2b7fd8a7
To make sure i did not miss a jpg, i change by test to not end until 050.jpg was found, instead of ending when fread returns a block smaller then 512 bytes. I was expecting a segmentation fault, or to find the missing image. What happen it that the last jpg 049.jpg got very big, a few megabytes big, and was not readable, but it never found a fifty first image. So i am sure it only 50 in the recover file.
So i am confused why check50 is telling me that :( recovers last file correctly.
This is the code i wrote.
#include <stdio.h>
#include <stdlib.h>
#define BLOCK 512
int main(int argc, char *argv[])
{
// ensure proper usage
if (argc != 2)
{
fprintf(stderr, "Usage: ./recover <disk name>\n");
return 1;
}
// remember filenames
char *infile = argv[1];
// open input file
FILE *inptr = fopen(infile, "r");
if (inptr == NULL)
{
fprintf(stderr, "Could not open %s.\n", infile);
return 2;
}
unsigned char buffer[BLOCK]; // storage for block from memory card.
int flag;
char filename[15];
int i=0;
FILE *img;
do {
flag = fread(buffer, sizeof(buffer), 1, inptr);
if ((buffer[0] == 0xff) &&
(buffer[1] == 0xd8) &&
(buffer[2] == 0xff) &&
((buffer[3] & 0xf0) == 0xe0))
{
if(i>0)
fclose(img);
//creat new jpeg
sprintf(filename, "%03d.jpg", i);
i++;
img = fopen(filename, "w");
fwrite(buffer, sizeof(buffer), 1, img);
}
else if (i == 0)
continue;
else
fwrite(buffer, sizeof(buffer), 1, img);
} while(flag == 1);
if(i)
{
fclose(img);
fclose(inptr);
}
return 0;
}