I've been stuck on Recover for the last few days. I'm trying to work out why Submit tells me none of the jpg files are found. I've revised it multiple times, worked it out on paper... I can't figure out what the problem is. Any help would be greatly appreciated!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
typedef uint8_t BYTE;
int main(int argc, char *argv[])
{
if (argc != 2)
{
fprintf(stderr, "Usage: ./recover image\n");
return 1;
}
char *infile = argv[1];
FILE *cardfile = fopen(infile, "r");
if (infile == NULL)
{
fprintf(stderr, "Could not open %s", infile);
return 2;
}
int r = 0;
int jpgcount = 0;
int c;
char filename[8];
FILE *jpgfile = NULL;
unsigned char bytes[512];
while((c = fgetc(cardfile)) != EOF)
{
fread(bytes, sizeof(BYTE), 512, cardfile);
if (bytes[0] == 0xff && bytes[1] == 0xd8 && bytes[2] == 0xff && (bytes[3]==0xe0 || bytes[3]==0xe1))
{
if(jpgcount >= 1)
{
fclose(jpgfile);
r++;
}
sprintf(filename, "%03i.jpg", r);
jpgfile = fopen(filename, "w");
if (jpgfile == NULL)
{
fprintf(stderr, "Could not create %s.\n", filename);
return 3;
}
jpgcount++;
}
if(jpgcount == 0)
{
continue;
}
fwrite(bytes, sizeof(BYTE), 512, jpgfile);
}
fclose(cardfile);
if(jpgfile != NULL)
{
fclose(jpgfile);
}
return 0;
}