here's the code i've reached so far. I know it has a lot of mistakes yet and it also gives me a segmentation fault but i'd really like to understand how to think about fixing it. thanks!
#include <stdio.h>
#include <stdint.h>
typedef uint8_t BYTE;
int main(void)
{
BYTE buffer[512];
FILE* card = fopen("card.raw", "r");
if (card == NULL)
{
fclose(card);
printf("couldn't open card.raw\n");
return 1;
}
char title[8];
FILE* jpeg = NULL;
fseek(card, 0, SEEK_SET);
do
{
fread(&buffer, sizeof(buffer), 1, card);
}
while ((buffer[0] != 0xff && buffer[1] != 0xd8 && buffer[2] != 0xff && buffer[3] != 0xe0) || (buffer[0] != 0xff && buffer[1] != 0xd8 && buffer[2] != 0xff && buffer[3] != 0xe1));
fseek(card, -512, SEEK_CUR);
while(fread(buffer, 512, 1, card) == 1)
{
fread(&buffer, sizeof(buffer), 1, card);
if ((buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && buffer[3] == 0xe0) || (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && buffer[3] == 0xe1))
{
if (jpeg != NULL)
{
fclose(jpeg);
}
int counter = 0;
sprintf(title, "%.3d.jpg", counter);
FILE* jpeg = fopen(title, "a");
if (jpeg == NULL)
{
fclose(jpeg);
printf("couldn't open jpeg file\n");
return 2;
}
fwrite(&buffer, sizeof(buffer), 1, jpeg);
counter++;
}
else
{
fwrite(&buffer, sizeof(buffer), 1, jpeg);
}
}
fclose(card);
return 0;
}