**** UPDATE I hope it is less a mess now. It recovers all images, not invalid anymore but images return chessboard patterns. Any leads why?
while (fread(&buffer, 512, 1, infile) != 0)
{
FILE *img = NULL;
if (buffer[0] == 0xff &&
buffer[1] == 0xd8 &&
buffer[2] == 0xff &&
(buffer[3] & 0xf0) == 0xe0)
{
if (img == NULL)
{
sprintf(filename, "%03i.jpg", count);
img = fopen(filename,"w");
fwrite(&buffer, 512, 1, img);
count++;
}
else if (img != NULL)
{
fclose(img);
sprintf(filename, "%03i.jpg", count);
img = fopen(filename, "w");
fwrite(&buffer, 512, 1, img);
count++;
}
}
else
{
if (img != NULL)
{
fwrite(&buffer,512,1,img);
}
else if (img == NULL)
{
continue;
}
}
}
fclose(infile);
}