0
#include <stdio.h>
#include <stdlib.h>
 
int main(int argc, char *argv[])
{

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

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

    if (f == NULL)
    {
        printf("File could not be opened\n");
        return 1;
    }

    unsigned char buffer[512];
    int count = 0;
    char filename[8];
    FILE *recovered = NULL;

    while (fread(buffer, 512, 1, f) == 1)
    {

        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        {
            if (count > 0)
            {
                fclose(recovered);
            }

            sprintf(filename, "%03i.jpg", count);
            recovered = fopen(filename,"w");
            fwrite(f, 512, 1, recovered);
            count++;
        }

        else if(count >= 1)
        {
           fwrite(f, 512, 1, recovered);
        }
    }

    fclose(f);
    fclose(recovered);
    return 0;
    
}
0

Hmmmm.....

        fwrite(f, 512, 1, recovered);

Shouldn't the data be copied from "buffer" instead of "f"?

| improve this answer | |

You must log in to answer this question.

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