I have been pulling my hair for recover 2 weeks by now. I finally got it to print 50 files at last. However, they are all invalid . Can anyone help me out?
while (fread(&buffer, sizeof(buffer), 1, infile) == 1) // if fread return anything smaller than 1 then it's EOF
{
//fread(&buffer, sizeof(buffer), 1, infile); (this line is causing files not reach 49 files, for some reasons)
//check if block is beginning of jpeg
if (buffer[0] == 0xff &&
buffer[1] == 0xd8 &&
buffer[2] == 0xff &&
(buffer[3] & 0xf0) == 0xe0)
{
//Create new file to store jpg
sprintf(filename, "%03i.jpg", count);
// Here we open the img file
FILE *img = fopen(filename, "w");
// if there is a JPEG file already opened
// close it.
// then open the next file
if (img != NULL)
{
fclose(img);
img = fopen(filename,"w");
}
//write to the new file 512 bytes/block a time while byte is not JPEG beginning
while (buffer[0] != 0xff &&
buffer[1] != 0xd8 &&
buffer[2] != 0xff &&
(buffer[3] & 0xf0) != 0xe0)
{
fwrite(&buffer, sizeof(buffer), 1, img);
}
count++;
fclose(img);
}
}
fclose(infile);
}
Thank you in advance.
**** 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);
}