Code does not recover correctly – CS50 Week4 Recover Problem

Issue

This Content is from Stack Overflow. Question asked by osterowsky

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

#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 IMAGEn");
        return 1;
    }

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

    if (card_file == NULL)
    {
        printf("Cannot read correctly the filen");
        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;

}



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?