As the title suggests my program finds only 28 images and they are unsupported format and won't open (the file names are correct though).
Please can anyone highlight any issues with my code or point me in the right direction.
I have used the debugger and it seems to be an issue with starting a new jpeg at the end of the old one.
Thanks a lot.
#include <cs50.h>
#include <stdio.h>
#define BLOCKSIZE 512
int main(int argc, char *argv[])
{
// check correct usage
if (argc != 2)
{
fprintf(stderr, "Usage: recover file\n");
return 1;
}
// address of argv[1] stored in infile
char *infile = argv[1];
// file pointer to open infile for reading
FILE *card = fopen(infile, "r");
// check file opens/exists
if (card == NULL)
{
fprintf(stderr, "Could not open %s.\n", infile);
return 2;
}
int j = 0;
int counter = 0;
// alocate memory for block
unsigned char block[BLOCKSIZE];
while(j < 1)
{
// read block from card and check for end of file
fread(block, 1, BLOCKSIZE, card);
if (fread(block, 1, BLOCKSIZE, card) == BLOCKSIZE)
{
// check for jpeg
if (block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff && (block[3] & 0xf0) == 0xe0)
{
// store file name
char jpegname[8];
sprintf(jpegname, "%03i.jpg", counter);
//open file (jpegfile is pointer to it)
FILE* jpegfile = fopen(jpegname, "w");
// write block from card to file
fwrite(jpegfile, 1, BLOCKSIZE, card);
int i = 0;
// for rest of jpg, keep writing blocks to image
while(i < 1)
{
fread(block, 1, BLOCKSIZE, card);
// check for end of file
if (fread(block, 1, BLOCKSIZE, card) == BLOCKSIZE)
{
if (block[0] != 0xff && block[1] != 0xd8 && block[2] != 0xff && (block[3] & 0xf0) != 0xe0)
{
fwrite(jpegfile, 1, BLOCKSIZE, card);
}
// until another jpg starts
else
{
fclose(jpegfile);
counter++;
// stop loop when new jpeg starts
i++;
// go to start of block ready for main loop
fseek(card, -BLOCKSIZE, SEEK_CUR);
}
}
// stop loop if end of file reached
else
{
fclose(jpegfile);
i++;
}
}
}
}
// stop main loop if end of file reached
else
{
j++;
}
}
}