I've looked at other forums on the question but it seems every solution varies based on how someone has written their code. So here's mine. Can someone show me the fault in my program.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
int main(int argc, char* argv[])
{
// open image file
FILE* file = fopen("card.raw", "r");
//buffer to store images in
unsigned char data [512];
//signs indicating an image
uint8_t FirstSign [4] = {0xff, 0xd8, 0xff, 0xe0};
uint8_t SecondSign [4] = {0xff, 0xd8, 0xff, 0xe1};
//keep track of number of images
int counter = 001;
FILE* img;
int n;
int h;
char title[7];
//check for end of file
while (fread(&data,512,1,file) == 1)
{
n = memcmp (FirstSign, data, sizeof(FirstSign));
h = memcmp (SecondSign, data, sizeof(FirstSign));
//find out if image is a JPEG
if (n == 0 || h == 0)
{
//create file to write to
sprintf(title,"%d.jpg",counter);
img = fopen(title,"a");
//keep track of the number of images
counter++;
//write images to current directory until a new image arrives
do
{
fwrite(&data, sizeof(data), 1, img);
fread(&data,512,1,file);
n = memcmp (FirstSign, data, sizeof(FirstSign));
h = memcmp (SecondSign, data, sizeof(FirstSign));
}while(n != 0 && h != 0);
// close file once finshed
fclose(img);
}
//if image is not a JPEG, close file
}
}