0

I am working on set4 recover

My program is always printing "Testing EOF", hence it looks card file is empty.

Here is my code in detail.

#include <stdio.h>

#include <stdlib.h>

#include <stdint.h>

typedef uint8_t BYTE;

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        printf("Usage: ./recover image\n");
        return 1;
    }

    FILE *card = fopen(argv[1], "r");
    if (card == NULL)
    {
        printf("Could not open card.\n");
        return 1;
    }

    FILE *new_card = fopen(argv[1], "w");
    if (new_card == NULL)
    {
        printf("new_card == NULL.\n");
        return 1;
    }
    unsigned char header[512];
    
    //BYTE header[512];
    char *name = malloc(8 * 512);
    int img_count = 0;
     fread(&header, 512, 1, card);

    // checking for if end of data on card has reached.
    if(feof(card))
    {
        printf("Testing EOF\n");
    }

    //while (fread(header, 512, 1, card))
    while (fread(&header, 512, 1, card) == 1)
    {
        printf("Test");
        if (header[0] == 0xff || header[1] == 0xd8 || header[2] == 0xff || (header[3] & 0xf0) == 0xe0)
        {
            printf("Entered");
            if(name != NULL)
            {
                fclose(new_card);
            }
            printf("Entered1");
            sprintf(name, "%03i.jpg", img_count);
            new_card = fopen(name, "w");
            img_count++;
            printf("Entered2");

        }
        else
        {
            if (new_card != NULL)
            {
                fwrite(header, 512, 512, new_card);
            }
        }
    }

    free(name);
    fclose(card);
    fclose(new_card);

}

Please look into it.

Thanks & regards Fatmah Faridi

1

The input file, card.raw is empty now. Look at the following code:

FILE *new_card = fopen(argv[1], "w");

As soon as you ran this code the first time, the input file card.raw was deleted and a new, empty file of the same name created. You can compare by running this command: ls -al card.raw

You need to remove this section of code from the source code file. Then, you'll need to re-download the card.raw file.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

0

You must log in to answer this question.

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