It has been quite a struggle just to get to this point, but I feel like I'm close. Hopefully it's just a small oversight, but I keep getting 24 blank jpgs. Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef uint8_t BYTE;
int main(void)
{
// open card.raw
FILE* card = fopen("card.raw", "r");
if (card == NULL)
{
printf("File could not be opened.\n");
return 1;
}
// set output file
FILE* out = NULL;
// set array for jpg file names
char temp[10];
// create buffer of 512 bytes
BYTE buffer[512];
// create counter for jpgs
int jpg = 0;
// loop to read card until end of card
while (!feof(card))
{
// read 512 byte block into buffer
fread(buffer, sizeof(buffer), 1, card);
// determine if block is a jpg
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff
&& (buffer[3] == 0xe0 || buffer[3] == 0xe1))
{
if (out == NULL)
{
// name file with 3 digits (00X)
sprintf(temp, "%03d.jpg", jpg);
// open file to write to
out = fopen(temp, "w");
// write buffer to jpg file
fwrite(buffer, sizeof(buffer), 1, out);
// move counter up for next file number
jpg++;
}
else
{
fclose(out);
out = NULL;
}
}
}
}