I'm pretty confused with recover.
I've seen in previous posts that it is wrong to use feof
, but the problem is that when I use fread
the images won't show. However, when I use feof
I can see all the images.
It seems that when I use fread the images wont write completily (they are smaller in size), but I can't find why that is.
I hope someone can help.
//define memory block
#define MBLOCK 512
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main(int argc, char* argv[])
{
//open card file
char* sdCard= "card.raw";
FILE* sdCardFile = fopen(sdCard, "r");
if (sdCardFile == NULL)
{
fclose(sdCardFile);
printf("Could not open %s\n", sdCard);
return 1;
}
typedef uint8_t BYTE;
BYTE buffer[MBLOCK];
int count = 0;
char outptr[8];
FILE* img;
//repead until end of card
while (fread(&buffer, MBLOCK, 1, sdCardFile) == 1)
{
//read 512 bytes into a buffer
fread(&buffer, MBLOCK, 1, sdCardFile);
if ((buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff) && (buffer[3] == 0xe0 || buffer[3] == 0xe1))
{
//if there is a jpeg opened, close it!
if (count > 0)
{
fclose(img);
printf("closed!");
}
//store that we've found a jpeg
count++;
//print the jpeg into the current directory
sprintf(outptr, "%03d.jpg", count - 1);
//now, I can open the jpeg and write 512 bytes at a time
img = fopen(outptr, "a");
printf("opened!");
fwrite(&buffer, MBLOCK, 1, img);
}
else
{
if (count > 0)
{
fwrite(&buffer, MBLOCK, 1, img);
printf(".");
}
}
}
fclose(sdCardFile);
}