0

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

typedef uint8_t BYTE;

int main(int argc, char *argv[]) { if (argc != 2) { printf("Usage: ./recover image\n"); return 1; } else { // This line is to open the card. FILE *memory = fopen(argv[1], "r"); if (memory == NULL) { printf("Error reading this file\n"); return 1; }

    // This file is to store whatever is before jpeg files.
    FILE *nothing_file = fopen("nothing_file", "w");

    int num_of_files = 0;
    // To use files that Im going to make I will say.
    FILE *new_file = NULL;
    // Im going to use (do while) loop because i want the compiler to proceed then check so if it got to the last part it will proceed then it will know that it reached the end of the file.
    unsigned char Buffer[512];
    int x;
    // This codition if the compiler found the begginning of the jpeg files.
    int condition = 0;
    do
    {
        // To hold a name of 7 digits I will do.
        char name[7];

        // To check if its the beginning of jpeg file.
        if ((unsigned char)Buffer[0] == 0xff && (unsigned char)Buffer[1] == 0xd8 && (unsigned char)Buffer[2] == 0xff && (((unsigned char)Buffer[3] & 0xf0) == 0xe0))
        {
            condition = 1;
            // To check if this is the first file.
            if (num_of_files == 0)
            {
                if (new_file != NULL)
                {
                    fclose(new_file);
                }
                sprintf(name, "%03i.jpg", num_of_files);
                new_file = fopen(name, "a");
                /*memcpy( new_file, memory, *Buffer);*/
                fwrite(&Buffer, 512, 1, new_file);
                num_of_files++;

            }
            else
            {
                // If it wasn`t the first file.
                fclose(new_file);
                sprintf(name, "%03i.jpg", num_of_files);
                num_of_files++;
                new_file = fopen(name, "a");
                /*memcpy( new_file, memory, *Buffer);*/
                fwrite(&Buffer, 512, 1, new_file);
            }
        }
        else
        {
            if (condition == 0)
            {
                
                /*memcpy( nothing_file, memory, *Buffer);*/
                fwrite(&Buffer, 512, 1, nothing_file);
                
            }
            else
            {
                // If it wasn`t the name of the file.
                /*memcpy( new_file, memory, *Buffer);*/
                fwrite(&Buffer, 512, 1, new_file);
            }
        }
    }while(fread(&Buffer, 512, 1, memory));
    

    // To close after finishing reading.
    fclose(new_file);
    fclose(memory);
}

}

1

You must log in to answer this question.

Browse other questions tagged .