My program runs properly through to the end of file but only recovers 13 images. Based on the fact that the jpegs are back to back and that my program reaches the end of the card.raw file, I'm assuming I must be missing the initial 3 photos. I've been staring at my code and using gdb for the past few days and cannot figure out what could possibly be wrong with it. Any help would be appreciated.
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
typedef uint8_t BYTE;
int main(int argc, char* argv[])
{
// ensure proper usage
if (argc != 1)
{
printf("Command line not recognized.\n");
return 1;
}
// open raw file
FILE* inptr = fopen("card.raw", "r");
if (inptr == NULL)
{
printf("card.raw file not available\n");
return 2;
}
// format filename for 00n notation
char filename[8];
BYTE store[512];
// read until jpg starts
while (store[0] != 0xff || store[1] != 0xd8 || store[2] != 0xff || store[3] != (0xe0 & 0xe1))
{
fread(store, 512, 1, inptr);
}
// start loop to make a new file for each image
for (int i = 0; i < 16; i++)
{
// store file name as a string to format properly
sprintf(filename, "%03d.jpg", i);
// create output files
FILE* outptr = fopen(filename, "w");
if (outptr == NULL)
{
fclose(inptr);
printf("File could not be created\n");
return 3;
}
// write the first line of the jpeg and move to the next block
fwrite(store, 512, 1, outptr);
if (feof(inptr))
{
break;
}
fread(store, 512, 1, inptr);
if (feof(inptr))
{
fwrite(store, 512, 1, outptr);
break;
}
// continue writing file until next jpeg is reached
while (store[0] != 0xff || store[1] != 0xd8 || store[2] != 0xff || store[3] != (0xe0 & 0xe1))
{
fwrite(store, 512, 1, outptr);
fread(store, 512, 1, inptr);
if (feof(inptr))
{
fwrite(store, 512, 1, outptr);
break;
}
}
fclose(outptr);
if (feof(inptr))
{
break;
}
}
fclose(inptr);
return 0;
}