I am having trouble getting recover to function in any capacity like it is supposed to. Any advice as to what I should fix?
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <cs50.h>
typedef uint8_t BYTE;
int main(void)
{
// Open input file
FILE* file = fopen("card.raw", "r");
//define variables
BYTE buffer[512];
int i = 0;
char title[8];
FILE* img = NULL;
//read entire memory card
while(fread(buffer, sizeof(BYTE), 512, file) == 512)
{
//if bytes match the jpeg sequence
if (buffer[0] == 0xff &&
buffer[1] == 0xd8 &&
buffer[2] == 0xff &&
(buffer[3] == 0xe0 ||buffer[3] == 0xe1))
{
if (i > 0)
{
//closes open jpeg file
fclose(img);
}
//names file correctly
sprintf(title, "%03d.jpg\n", i);
i++;
//opens a jpeg file of the correct name
img = fopen(title, "a");
//writes jpeg to file
fwrite(buffer, sizeof(BYTE), 512, img);
}
else
{
if (i > 0)
{
//continues writing jpeg to file until a new jpeg is found
fwrite(buffer, sizeof(BYTE), 512, img);
}
}
}
// the end
fclose(img);
fclose(file);
return 0;
}