My code is recovering 50 images, but according to check50 the images are not being recovered correctly. A few of the images are obviously not recovering but I can't figure out why in my code. Several of the recovered images are clearly showing the left side of the image on the far right as an example of the obvious issues.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef uint8_t BYTE;
int main(int argc, char *argv[])
{
// Check to make sure that user submitted a file to recover images from
if (argc != 2)
{
fprintf(stderr, "Invalid file\n");
return 1;
}
// Establish the infile as the submitted file
char *infile = argv[1];
// Open input file
FILE *f = fopen(infile, "r");
// Verify that input file opened
if (f == NULL)
{
fprintf(stderr, "Could not open %s \n", infile);
return 1;
}
// Establish variables to support the while loop array
// Array of 512 bytes to store the data read from the card, array to store the filename, counter to keep track of how many JPEGs found, out file
BYTE buffer[512];
char *filename[7];
int counter = 0;
FILE *img;
// loop through the memory card in 512 byte chunks when number != 1 then end of file is reached
while (fread(buffer, sizeof(buffer), 1, f) == 1 )
{
// Check if the read block is the start of a jpeg
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
// check if its the first jpeg
if (counter != 0)
{
fclose(img);
}
sprintf(*filename, "%03i.jpg", counter);
img = fopen(*filename, "w");
fwrite (&buffer, sizeof(buffer), 1, img);
counter++;
}
// Need to determine if a JPEG has already been found
else if (img)
{
fwrite (&buffer, sizeof(buffer), 1 , img);
}
}
fclose(img);
fclose(f);
return 0;
}
Can someone help me understand what could be causing my images to not recover correctly? I tried to debug but it's giving me an error when I try to step through the writing of the image files that I'm not getting when I just run it.