All 50 valid images are created, but the program did not pass Check50.
This is my updated code:
/** * recover.c * * Computer Science 50 * Problem Set 4 * * Recovers JPEGs from a forensic image. */
include
include
int main(void) { // open CF card FILE* CF_card = fopen("card.raw", "r"); if (CF_card == NULL) { fclose(CF_card); printf("Could not open %s.\n", "card.raw"); return 1; }
// define a counter that counts the number of JPEGs found, a buffer, a character that denotes a filename, and a global variable for the output file
int JPEG_counter = 0;
uint8_t buffer[512];
char filename[8];
FILE* img = NULL;
// read CF card
while(fread(buffer, sizeof(buffer), 1, CF_card) == 1)
{
// JPEG signature hunt
if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] >= 0xe0 && buffer[3] <= 0xef))
{
if (img != NULL)
{
fclose(img);
}
// JPEG found, create new file
sprintf(filename, "%03d.jpg", JPEG_counter);
// open newly created file
img = fopen(filename, "w");
// keep track of JPEG found
JPEG_counter++;
// write to newly created file
fwrite(buffer, sizeof(buffer), 1, img);
}
// continue to write to the same file until the next JPEG is found
else if (JPEG_counter > 0)
{
fwrite(buffer, sizeof(buffer), 1, img);
}
else
{
fread(buffer, sizeof(buffer), 1, CF_card);
}
}
// close the file
fclose(CF_card);
return 0;
}
This is the Check50 report:
~/workspace/pset4/jpg/ $ check50 2015.fall.pset4.recover recover.c :) recover.c exists :) recover.c compiles :( recovers 000.jpg correctly \ expected 000.jpg to exist :( recovers middle files correctly \ expected 001.jpg to exist :( recovers last file correctly \ expected 015.jpg to exist https://sandbox.cs50.net/checks/096138a2dccf48b6af8e2311acacced8