0

I ran my code through the compiler and the recovered images are all blank. The images do not match when I ran my code through check50 as well. I can't identify the problem with my code. I would appreciate it if someone could help me out. Thank you :)

#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>  
#include <stdint.h>
#include <string.h>

typedef uint8_t BYTE;

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

//Open memory card file
FILE* inptr = fopen(argv[1], "r");
//Ensure that file is not NULL
if(argv[1] == NULL)
{
    fprintf(stderr, "File %s could not be opened.\n", argv[1]);
    return 2;
}

//Declaration of buffer array
BYTE buffer[512];
int n = 0;
char filename[8];


//Declaration of outfile
FILE* outptr = NULL;
//Till end of file

    //Reading into memory card
    while(fread(buffer, 512, 1, inptr))
    {
        //Finding for start of JPEG
        if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        {
            //If JPEG is still open
            if(outptr != NULL)
            {
                //Closing of previous JPEG once new JPEG found
                fclose(outptr);
                n++;
                //Creation of new JPEG with filename
                sprintf(filename, "%03i.jpg", n);
                //Opening of outptr again to write in new JPEG this time
                outptr = fopen(filename, "a");
                fwrite(&buffer, 512, 1, outptr);
            }
            //If JPEG not yet open (first JPEG to be opened, explaining why outptr is still NULL)
            else
            {
                //Creation of new JPEG with filename
                sprintf(filename, "%03i.jpg", n);
                //Opening of outptr again to write in new JPEG this time
                outptr = fopen(filename, "a");
                fwrite(&buffer, 512, 1, outptr);
            }
        }
    }


//Closing of infile and outfile
fclose(inptr);
return 0;



}
0

While this code may be finding signatures and opening files as it should, the problem is with the data blocks that don't start with a signature. There is no code to add them to the image file, so they are being discarded.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Wow, thank you so much! I finally figured out where I went wrong :) – Prav Elan May 16 '18 at 11:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .