0

I am on problem set 4 recover and I have nearly completed it. I have written my code and it compiles but when I run it on the card.raw file it appears to recover all the 50 photos except the 1st one appears to be a blank image. I ran check 50 and it says my code is incorrect as well. I'm sure im making some sort of small mistake but I just can't figure out what.

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

typedef uint8_t BYTE;

int main(int argc, char *argv[])
{
  //if more than 1 CLA remind user how to use and return 1
    if (argc != 2)
    {
        printf("Usage: ./recover input\n");
        return 1;
    }
     // open memory card file
    FILE *memory = fopen(argv[1], "r");
 //if image cannot be opened for reading inform user and return 1
    if(!memory)
    {
        printf("ERROR: Unable to open");
        return 1;

    }

    //create int to keep track of of jpgs
    int jpgnum = 0;
    //malloc buffer space for filename
    char *filename = malloc(8* sizeof(char));

    //create buffers
    FILE *img = NULL;
    BYTE buffer[512];
     //loop until end of file + read a Block
    while( fread(&buffer, 512, 1, memory) == 1)
    {
        //look at byte if it is the start of a new jpeg
        if((buffer[0] == 0xff) && (buffer[1] == 0xd8) && (buffer[2] == 0xff) && (buffer[3] & 0xf0) == 0xe0)
        {
 //if first JPEG start writing file at name 000
            if(jpgnum == 0)
            {
                sprintf(filename, "%03i.jpg", jpgnum);
                img = fopen(filename, "w");
                fwrite(buffer, sizeof(BYTE), 512, img);
                jpgnum++;
            }
 //if not first but is new JPEG close last file and write new JPEG
            if(jpgnum > 0)
            {
                fclose(img);
                sprintf(filename, "%03i.jpg", jpgnum);
                img = fopen(filename, "w");
                fwrite(buffer, sizeof(BYTE), 512, img);
                jpgnum++;

            }
        }
 //if not start of new file continue writing to old file
        else if(img != NULL)
        {
            fwrite(buffer, sizeof(BYTE), 512, img);
        }
    }
 //close any remaining files.
    fclose(img);
    fclose(memory);
 //free malloc'd space
 free(filename);


}

1 Answer 1

1

So, the first file is bad. That should be a clue that there's something wrong with how the first file is processed. Did you step through the program using debug50 and watch which statements execute? If not, you really should do that to get the practice and experience. (Seriously, it'll help you learn to identify logic errors.)

Let's think carefully about what happens when the first signature is found.

The if(buffer...) statement passes, so the code drops to the next if statement. It sees that jpgnum is 0, so it executes the code to open the first file and copies the buffer to the output file, and increments jpgnum to 1.

Now here's where it gets interesting. The code drops out of that if statement code and proceeds to the next if statement, if(jpgnum > 0) Surprise! It is greater than 0! Remember how it was incremented just a few lines of code earlier? So, it closes the first file and opens the second file, 001.jpg. It then copies the same buffer out to that file and the program continues from there.

There's something else that escaped your attention. There are probably 51 output files, not 50. The first one is 000.jpg. You probably have a file called 050.jpg. That file shouldn't exist. Did you miss that?

So, what do you think. Should that if(jpgnum > 0) statement maybe be something else, so that only one of those two if statements execute? ;-)

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

1
  • wow thanks for that I can't believe I missed that. I'll have to go back try to find the info on the debugger again so I can use that more effectively. Jun 19, 2021 at 17:26

You must log in to answer this question.

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