Recover perfectly recovers all 16 jpg
s, but fails the last 3 checks in check50. https://sandbox.cs50.net/checks/d8b30ce483964703917dc0153b9d0d2e
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
typedef uint8_t BYTE;
int main(int argc, char* argv[])
{
// open file
FILE* input = fopen("card.raw", "r");
// check for error while opening file
if (input == NULL)
{
printf("Couldn't open file.");
fclose(input);
return 1;
}
BYTE buf[512];
int file_counter = 0;
FILE* jpg = NULL;
// read file until EOF reached
while (!feof(input))
{
fread(&buf, 512, 1, input);
if (buf[0] == 0xff && buf[1] == 0xd8 && buf[2] == 0xff &&
(buf[3] == 0xe0 || buf[3] == 0xe1))
{
char jpg_name[4];
sprintf(jpg_name, "0%i.jpg", file_counter);
jpg = fopen(jpg_name, "w+");
file_counter++;
}
if (jpg != NULL)
{
fwrite(&buf, 512, 1, jpg);
}
else
{
continue;
}
}
fclose(jpg);
fclose(input);
return 0;
}