0

I have been stuck at this code for quiet a while now and i am finally able to generate all the images but the images are pixalated and not correct . I have seen online as why this might happen and changed my code but this is not helping me at all.

I am getting following messages from check50

:) recover.c exists.

:) recover.c compiles.

:) handles lack of forensic image

:( recovers 000.jpg correctly recovered image does not match

:( recovers middle images correctly recovered image does not match

:( recovers 049.jpg correctly recovered image does not match

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
//#include <stdint.h>
typedef unsigned char BYTE;

int main(int argc, char *argv[])
{
    if (argc!=2)
    {
        printf("Usage: ./recover image");
        return 1;
    }

    FILE *file = fopen(argv[1],"r");
    if(file ==NULL)
    {
        printf("cannot be opened");
        return 1;
    }

    //for (   ) //here we read till it reaches end of file i.e able to read 512 bytes and not less



    BYTE bytes[512]; // defining buffer
    char filename[8];
    int count = -1;
    FILE *img = NULL;
    //bool flag = false;
    bool new_file = false;

    while ((fread(bytes,512,1,file))==1)
    {
        if((bytes[0] == 0xff) &&(bytes[1] == 0xd8) && (bytes[2] == 0xff) && ((bytes[3] & 0xf0) == 0xe0))
    {
        new_file = true;


        count++;
        sprintf(filename,"%03i.jpg",count);
        img = fopen(filename,"w");
        fwrite(bytes,512,1,img);
        fclose(img);




        if (new_file == false)
        {
           img = fopen(filename,"a");
           fwrite(bytes,512,1,img);
           fclose(img);


        }


    }    
}

fclose(file);
//fclose(img);
return 0;

}

Please suggest the changes or bugs in the code. :)

You must log in to answer this question.

Browse other questions tagged .