I had been working on this for 6+ hours and could not figure out what was going on. All 50 images are produced, but when I opened them, I saw no pictures in it. What is wrong? Please help me out, and thank you so much!!!!! Here are the codes:
/**
* recover.c
*
* Computer Science 50
* Problem Set 4
*
* Recovers JPEGs from a forensic image.
*/
#include <stdio.h>
#include <stdint.h>
int main(void)
{
// open CF card
FILE* CF_card = fopen("card.raw", "r");
if (CF_card == NULL)
{
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, "%.3d.jpg", JPEG_counter);
// open newly created file
img = fopen(filename, "w");
if (img == NULL)
{
fclose(CF_card);
fprintf(stderr, "Could not create %s.\n", filename);
return 2;
}
// keep track of JPEG found
JPEG_counter++;
// write to newly created file
fwrite(&buffer, 512, 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);
}
}
// close the file
fclose(CF_card);
return 0;
}