This is what I wrote for pset4 recover. The problem is that I am only getting first 2 images (partially, not full images). And also it creates like hundreds of images and says "file size limit exceeded." I thought my outer while loop would stop inner code once fread with 512 bytes and 1 block doesn't work. I think I am kind of close...any hints? Thank you.
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Enter file name!");
}
FILE *inptr = fopen (argv[1], "r");
if (inptr == NULL)
{
printf("File does not exist.");
return 1;
}
typedef uint8_t BYTE;
BYTE buffer[512];
char filename[8];
int counter = -1;
//loop until fread fails.
while (fread (buffer, 512, 1, inptr) == 1)
{
do
{
fread (buffer, 512, 1, inptr);
}
while (buffer[0] != 0xff &&
buffer[1] != 0xd8 &&
buffer[2] != 0xff &&
(buffer[3] & 0xf0) != 0xe0);
counter ++;
sprintf(filename, "%03i.jpg", counter);
FILE *img = fopen(filename, "w");
fwrite (buffer, 512, 1, img);
do
{
fread (buffer, 512, 1, inptr);
fwrite (buffer, 512, 1, img);
}
while (buffer[0] != 0xff &&
buffer[1] != 0xd8 &&
buffer[2] != 0xff &&
(buffer[3] & 0xf0) != 0xe0);
fclose(img);
}
fclose (inptr);
}