Code does not recover correctly - CS50 Week4 Recover Problem

Viewed 28

Does somebody has any ideas why code is not working properly? It outputs all the 50 recovered images in proper names, but I still got this:

:) 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
:| program is free of memory errors
    can't check until a frown turns upside down

My code is

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

typedef uint8_t BYTE;


int main(int argc, char *argv[])
{

    if (argc != 2)
    {
        printf("Usage: ./recover IMAGE\n");
        return 1;
    }

    FILE *card_file = fopen(argv[1], "r");

    if (card_file == NULL)
    {
        printf("Cannot read correctly the file\n");
        return 2;
    }

    const int BLOCK_SIZE = 512;
    BYTE buffer [BLOCK_SIZE];
    int i = 0;
    FILE *output = NULL;

    while (fread(buffer, BLOCK_SIZE, 1, card_file) == BLOCK_SIZE)
    {
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        {

            char filename[8];
            sprintf(filename, "%03i.jpg", i);

            output = fopen(filename, "w");
            fwrite(buffer, BLOCK_SIZE, 1, output);

            ++i;
            fclose(output);
        }
    }

    fclose(card_file);
    return 0;

}
0 Answers
Related