A little background first:
I really enjoyed figuring out recover.c. After finishing the successfully once, I wanted to cement the concepts in my head, so I rewrote it from scratch as best I could, and then debugged it. One single hangup was killing my program, and when I finally figured it out, my second iteration of recover.c worked perfectly.
That hangup was this: FILE* img;
FILE* img;
typedef uint8_t BYTE;
BYTE buffer[BLOCK];
int jpgCount = 0;
char filename;
while (fread(&buffer, BLOCK, 1, inptr))
{
if (buffer[0] == 0xff &&
buffer[1] == 0xd8 &&
buffer[2] == 0xff &&
(buffer[3] & 0xf0) == 0xe0)
{
if (jpgCount > 0)
{
fclose(img);
}
sprintf(&filename, "%03i.jpg", jpgCount);
img = fopen(&filename,"w");
fwrite(&buffer, BLOCK, 1, img);
jpgCount++;
}
else if (jpgCount > 0)
{
fwrite(&buffer, BLOCK, 1, img);
}
}
When I initially wrote the problem I used FILE *img
. The program would recognize jpg's and create jpg files, but they would all be corrupt. However, upon changing FILE *img
to FILE* img
, it worked.
Why was this happening? Maybe this is a simple question but I've read through the FILE manual pages and looked online but I can't seem to find the answer. Any guidance would be immensely appreciated for my understanding.
Thanks Dylan