so I was trying to solve this recover problem from cs50.IT compiles well but it shows me this following problem
:) recover.c exists.
:) recover.c compiles.
:) handles lack of forensic image
:( recovers 000.jpg correctly
000.jpg not found
:( recovers middle images correctly
001.jpg not found
:( recovers 049.jpg correctly
049.jpg not found
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef uint8_t BYTE;
int main(int argc, char *argv[])
{
FILE *file = fopen(argv[1],"rb");
// if command line arguments are not appropriate
if (argc != 2)
{
return 1;
}
if (file == NULL)
{
return 1;
}
int const BLOCK_SIZE = 512;
int count = 0 ;
char filename[9];
FILE *img = NULL;
BYTE buffer[BLOCK_SIZE];
//going through loop to write the lost files
while(fread(buffer , sizeof(BLOCK_SIZE) , 1 , file) == 512 )
{
if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
{
if (count == 0)
{
// first jpeg
sprintf(filename , "%03i.jpeg" , count );
img = fopen(filename,"w");
fwrite(buffer , sizeof(buffer) , 1 , img);
count++;
}
else if (count > 0 && buffer[0] != 0xff && buffer[1] != 0xd8 && buffer[2] != 0xff && (buffer[3] & 0xff) != 0xe0 )
{
//write new one
fclose(img);
sprintf(filename, "%03i.jpeg" , count);
img = fopen(filename , "w");
fwrite(buffer , sizeof(buffer) , 512 , img);
count++;
}
else if (count > 0)
{
//keep wiritng the current image
fwrite(buffer , sizeof(buffer) , 512 , img);
}
}
fclose(img);
fclose(file);
}
}