In my pseudo code this is what this will do(I made FILE* img a prototype also counter = 0 and newcounter is 2 initially ) read the first 512 bytes check if the header is in it if it is say found one. Increase counter and decrease newcounter. then check if counter is greater than new counter 1 is not > 1 and hence it will not close the file the first time a new jpeg is found. Then it will make a title as 001. Then it will check if counter is greater than 0 if it is open image name title and then write to img. It will loop again with next 512 bytes and then since counter is greater than 0 it will write 512 bytes more. However once a new header is found it will close the file and then restart the whole process. However I am getting a segfault after just making one jpeg with 0 bytes
this is my code
while(fread(buffer, 512, 1, card) == 1)
{
fread(buffer, 512, 1, card);
for(int c = 0; c < 512 ; c++)
{
if(c == 3 && (buffer[c] == 0xe1 || buffer[c] == 0xe0) && buffer[c - 1] == 0xff && buffer[c - 2] == 0xd8 && buffer[c - 3] == 0xff)
{
//new file detector
printf("found one\n");
counter++;
newcounter--;
if(counter > newcounter)
{
fclose(img);
}
sprintf(title, "%.3d.jpg", counter);
}
}
if (counter != 0)
{
FILE* img = fopen(title ,"a");
fwrite(buffer, 512, 1, img);
}
}