0

My program recovers the images but the recovered images are all blank and check50 says that they "don't match". All the images are 512 bytes in size. Please help me find out why this is happening. My code is attached below:

#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

int main(int argc, char *argv[])
{

    //check for proper usage
    if (argc != 2)
    {
        printf("Usage: ./recover image");
        return 1;
    }

    //initialization
    char filename[8] = {'#', '#', '#', '.', 'j', 'p', 'g'};

    //open memory card
    FILE *mc = fopen(argv[1], "r");

    if (mc == NULL)
    {
        //memory card doesn't exist
        return 1;
    }

    //initialization of number of jpegs
    int nofjpegs = 0;

    bool firstjpeg = true;

    //initialization
    FILE *img = NULL;

    unsigned char storagearray[512];

    //read memory card
    fread(storagearray, 512, 1, mc);

    //while end of file is not reached
    while (fread(storagearray, 512, 1, mc) == 1)
    {
        //check for the beginning of a jpeg
        if (storagearray[0] == 0xff && storagearray[1] == 0xd8 && storagearray[2] == 0xff && (storagearray[3] & 0xf0) == 0xe0)
        {
            if (firstjpeg == true)
            {
                sprintf(filename, "%03i.jpg", nofjpegs);
                nofjpegs++;
                firstjpeg = false;
            }
            else
            {
                //close the existing jpeg and make a new one
                fclose(img);    
                sprintf(filename, "%03i.jpg", nofjpegs);
                nofjpegs++;
            }

            img = fopen(filename, "w");
            fwrite(storagearray, 512, 1, img);
        }
    }

    if (nofjpegs > 0)
    {
    fclose(img);
    }
    fclose(mc);
}

Below is the edited code:

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

int main(int argc, char *argv[])
{

    //check for proper usage
    if (argc != 2)
    {
        printf("Usage: ./recover image");
        return 1;
    }

    //initialization
    char filename[8] = {'#', '#', '#', '.', 'j', 'p', 'g'};

    //open memory card
    FILE *mc = fopen(argv[1], "r");

    if (mc == NULL)
    {
        //memory card doesn't exist
        return 1;
    }

    //initialization of number of jpegs
    int nofjpegs = 0;

    bool firstjpeg = true;

    //initialization
    FILE *img = NULL;

    unsigned char storagearray[512];

    //read memory card
    fread(storagearray, 512, 1, mc);

    //while end of file is not reached
    for (int i = 0; i < sizeof(argv[1]); i++)
    {
        //check for the beginning of a jpeg
        if (storagearray[i] == 0xff && storagearray[i + 1] == 0xd8 && storagearray[i + 2] == 0xff && (storagearray[i + 3] & 0xf0) == 0xe0)
        {
            if (firstjpeg == true)
            {
                sprintf(filename, "%03i.jpg", nofjpegs);
                nofjpegs++;
                firstjpeg = false;
            }
            else
            {
                //close the existing jpeg and make a new one
                fclose(img);    
                sprintf(filename, "%03i.jpg", nofjpegs);
                nofjpegs++;
            }

            img = fopen(filename, "w");
            fwrite(storagearray, 512, 1, img);
        }
    }

    if (nofjpegs > 0)
    {
    fclose(img);
    }
    fclose(mc);
}
1

The code is checking for the signature in the first 4 bytes, but what about the code blocks that follow? I don't see any code that will continue to write subsequent blocks to the currently open file.

side note: what would happen if that first block read had a signature?

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

| improve this answer | |
  • Thanks for the answer. I've edited my code as above. I used a for loop instead of the while loop. This time, the error says: :( recovers 000.jpg correctly 000.jpg not found :( recovers middle images correctly 001.jpg not found :( recovers 049.jpg correctly 049.jpg not found – Areen Mar 18 at 18:05
  • Where are your fopen() statements? – Cliff B Mar 18 at 18:18
  • I think I have all my fopen() statements in the correct places. If i'm missing any statements or if they are in the wrong places, please tell me. – Areen Mar 18 at 18:22
  • My bad, I missed the fopen statement. Different issue. You switched to a for loop, using sizeof(argv[1]). Do you know what that actually returns? Add this to your code: printf("size: %lu\n",sizeof(argv[1])); Why not use a while statement and look for the end of the input file? – Cliff B Mar 18 at 19:49
  • I replaced the for loop with these lines, but this time, there's a segmentation fault: int i = 0; while (fread(storagearray, 512, 1, img) == 1) { i++; Sorry for the bad formatting. – Areen Mar 19 at 8:54

You must log in to answer this question.

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