0

All check correct and it recovers all 50 images but I have this problem and I can't solve it. I know I must find return number from fread function but the return number is 1 no all the elements that correct

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

bool found = false;
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        printf("Usage: ./recover image\n");
        return 1;
    }
    else
    {
        int count = 0, n = 0;
        FILE *f = fopen(argv[1],"r");
        int len;
        fseek(f, 0, SEEK_END);
        len = ftell(f);
        rewind(f); // to back to start again
        unsigned char buffer[len];
        FILE* img = NULL;

        for (int i = 512; i < len; i += 512)
        {
            int num = fread(buffer, 512, 1, f);
             //check first four byte
            if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
            {
                if (n == 0)
                {
                    found = true;
                    n = 1;
                    sprintf(argv[1], "%03i.jpg", count);
                    img = fopen(argv[1], "w");
                    fwrite(buffer, 512, 1, img);
                }
                else
                {
                    fclose(img);
                    count++;
                    sprintf(argv[1], "%03i.jpg", count);
                    img = fopen(argv[1], "w");
                    fwrite(buffer, 512, 1, img);
                }
            }
            else
            {
                if (found == true)
                {
                    fwrite(buffer, 512, 1, img);
                }
                else
                {
                    continue;
                }
            }
        }
        fclose(f);
    }
}

enter image description here

1

Please define your buffer with a length of 512, not len. In the current state it won't be a problem, but fixing your 049.jpg issue will change that.

Also, don't use argv[1] for sprintf. You don't know how many bytes you can write there safely. Create your own char array, with space for at least 8 characters (7 plus null terminator).

Now to your problem: You iterate over (len-512)/512 blocks (assuming a file length that's a multiple of 512). But it should be len/512, your last file is 512 bytes short. You could fix this by starting at int i = 0.

An alternative would be using a while loop, with the fread inside the condition, like while (fread(buffer, 512, 1, f) == 1)

3
  • I have two questions first one in sprintf the first array represent of what or what is inside it the second one why you say the array of size 8 how do you know the size of the array must be – Mahmoud Mohamed Feb 21 '20 at 19:31
  • I don't understand your first question. For numbers from 0 to 999, sprintf(filename, "%03i.jpg", count); will write 7 characters (three digits, one dot, three letters) and one null terminator, each of those requiring one byte. That means it should be at least a char filename[8]; or equivalent. If you don't feel like counting, you could also do char filename[] = "000.jpg";, this would pick the array length matching the initialisation string. – Blauelf Feb 21 '20 at 19:53
  • Thank you very much. – Mahmoud Mohamed Feb 21 '20 at 20:37

You must log in to answer this question.

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