0

I am not sure what is the problem in my code that is preventing the images from being recovered from the card.raw file. I've looked through my code and can't find the problem. Really appreciate it if someone here could help me out. Thank you :) Here's the link to the check50 that I did not pass: https://cs50.me/checks/2060ba9f074f5c5fd678c2e9260ff910e0288726

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

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
int buffer[512];
int n = 0;
char filename[8];


//Declaration of outfile
FILE* outptr = NULL;
//Till end of file
while(!feof(inptr))
{
    //Reading into memory card
    while(fread(buffer, 512, 1, inptr) == 1)
    {
        //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, "w");
                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, "w");
                fwrite(&buffer, 512, 1, outptr);
            }
        }
    }
}

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



}
1

Well, the first clue is that check50 isn't finding any of the output files. That should have been followed by an ls -al *.jpg to see what, if any files there were. It would have shown that no jpeg files were created.

The problem is simple, but only if you know what to look for. The issue lies here:

int buffer[512];

Remember, an int is 8 bytes in a 64 bit system, but the code is treating it as if each member of buffer is only 1 byte. Because buffer is of type int, the first 8 bytes of the file are being loaded into the buffer[0], so the test for the signature will always fail.

The fix is to use a data type that looks like a single, unsigned byte.... maybe a uint8_t?

Of course, after that's fixed, what are you going to do with the data that follows the signature? Image files are rarely just 512 bytes. ;-)

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

4
  • Wow, I finally understand where you are coming from. Was completely unaware of the mistake that I made. Thank you :) – Prav Elan May 13 '18 at 6:51
  • However, when I ran the program through the compiler, the 50 recovered images all turned out to be blank. Where did I go wrong? D: – Prav Elan May 13 '18 at 7:01
  • I have no idea since I can't see your current code and my crystal ball is in the shop. ;-) Sounds like time to post a new question. – Cliff B May 13 '18 at 7:26
  • Alright, will post a new question :) – Prav Elan May 13 '18 at 7:50

You must log in to answer this question.

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