I've recovered all 50 JPEGs and when I download them to my computer and change the name from (for example) 000.jpg%OA I can see the picture correctly. However, in the IDE,if I open the same JPEG I get hundreds of lines of garbage code. Any idea why I can't see the JPEGs in the IDE and can't pass the check50?
my code:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main (int argc, char *argv[]) {
typedef uint8_t HEXHOLDER;
int foundPhoto = 0;
if (argc != 2) {
fprintf(stderr, "Please enter one command line argument\n");
return 1;
}
char *infile = argv[1];
char *outfile = malloc(sizeof(char) * 8);
FILE *inptr = fopen(infile, "r");
if (inptr == NULL) {
fprintf(stderr, "It looks like the file you entered does not exist. Please try again.\n");
return 2;
}
HEXHOLDER buffer[512];
FILE *outptr = NULL;
while (fread(buffer, 1, 512, inptr) == 512)
{
fseek(inptr, -512, SEEK_CUR);
fread(buffer, 1, 512, inptr);
if (buffer[0] == 0xff &&
buffer[1] == 0xd8 &&
buffer[2] == 0xff &&
(buffer[3] & 0xf0) == 0xe0) {
if (foundPhoto == 0) {
sprintf(outfile, "%03i.jpg\n", foundPhoto);
outptr = fopen(outfile, "w");
fwrite(buffer, 1, 512, outptr);
foundPhoto++;
}
else if (foundPhoto > 0) {
fclose(outptr);
sprintf(outfile, "%03i.jpg\n", foundPhoto);
outptr = fopen(outfile, "w");
fwrite(buffer, 1, 512, outptr);
foundPhoto++;
}
}
else if (foundPhoto > 0) {
fwrite(buffer, 1 , 512, outptr);
}
else {
}
}
return 0;
}
Thanks a lot for your help!