1

I can't figure out what I'm doing wrong, please help!

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

#define BUFFER_SIZE 512

int main(void)
{
    // open memory card file
    FILE* input = fopen("card.raw", "r");
    if (input == NULL)
    {
        printf("Could not open card.raw.\n");
        return 2;
    }
    
    // create buffer
    unsigned char buffer[BUFFER_SIZE];
    
    // filename counter
    int filecount = 0;
    
    FILE* picture = NULL; 
    
    // check if we've found a jpeg yet or not
    int jpg_found = 0; //false
    
    // go through cardfile until there aren't any blocks left
    while (fread(buffer, BUFFER_SIZE, 1, input) == 1)
    {
        // read first 4 bytes of buffer and see if jpg signature using bitwise on last byte
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xe0) == 0xe0)
        {
            if (jpg_found == 1)
            {
                // We found the start of a new pic so close out current picture
                fclose(picture);
            }
            else
            {
                // jpg discovered and now we have the green light to write
                jpg_found = 1;
            }
            
            char filename[8];
            sprintf(filename, "%03d.jpg", filecount);
            picture = fopen(filename, "a");
            filecount++;
        }
        
        if (jpg_found == 1)
        {
            // write 512 bytes to file once we start finding jpgs
            fwrite(&buffer, BUFFER_SIZE, 1, picture);
        }
        
    }

    // close files
    fclose(input);
    fclose(picture);

    return 0;
}

This is the error I get:

:) 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

1 Answer 1

1

Looks like you've got a typo in the signature checking section. you've got

  • && (buffer[3] & 0xe0) == 0xe0) when you should have
  • && (buffer[3] & 0xf0) == 0xe0)

you have an e where you should have an f.

try changing that and see if you have an improvement

2
  • i fixed that, the problem still persists Commented Aug 26, 2020 at 11:10
  • is there anything else I can do? Commented Aug 26, 2020 at 11:11

You must log in to answer this question.

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