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

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

    // check if the card.raw was provided
    if (strcmp(argv[1], "card.raw") != 0)
    {
        printf("Invalid memory card\n");
        return 2;
    }

    // open the memory card
    FILE *file = fopen(argv[1], "r");
    // Check if there are images
    if (file == NULL)
    {
        return 3;
    }

    // Allocate memory/buffer named bytes with 512 bytes
    int counter = 0;
    unsigned char bytes[512];
    FILE *img = NULL;

    // read three bytes from file 
    while(!feof(file))
    {
        fread(bytes, sizeof(file), 1, file);

        // check if file is a jpeg
    
        if (bytes[0] == 0xff && bytes[1] == 0xd8 && bytes[2] == 0xff && (bytes[3] & 0xf0) == 0xe0)
        {
            // Check if there are full images then close
            if ( counter > 0)
            {
                fclose(img);
            }
            
            // Name the images
            char picture[8];
            sprintf(picture, "%03i.jpg", counter);
            img = fopen(picture, "w");
            
            // If there is no image return an error
            if (img == NULL)
            {
                printf("Image not read\n");
                return 1;
            }
            
            // Write the image
            fwrite(bytes, 512, 1, img);
            // increase the counter(image number)
            counter ++;
            
        }
        else if (img != NULL)
        {
            //keep writting in the current file
            fwrite(bytes, 512, 1, img);
        }
 
    }

    fclose(img);
    fclose(file);
}
  • ohh i managed to fix it just replaced sizeof(file) with 512. – Dynamo Denis Oct 3 at 12:28
0

Look at the following:

    fread(bytes, sizeof(file), 1, file);

The code is supposed to read in 512 bytes at a time. How much data does this read?

Side note: Checking that the name of the input file is "card.raw" is the same as hardcoding the input file name. The program should use whatever input file name that may be given. If check50 were to use a different file name in a test, it will fail. In fact, in some tests, check50 DOES use different file names for various tests.

There's another problem waiting in the wings related to the last file, but I'll let you find that one on your own first. If you can't resolve it, then it's time for a new question. ;-)

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

| improve this answer | |
  • Thank you even if its a bit late but I think the problem you expected me to encounter was when using the while loop while(!feof(file)) which I changed by passing in the read file while(fread(bytes, sizeof(f512), 1, file)); – Dynamo Denis Oct 10 at 14:06
  • It was. Glad you solved it on your own. ;-) – Cliff B Oct 10 at 16:40

You must log in to answer this question.

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