0

when i run the cod i can see the files but when i open it i can't see any pictures and also not all of the 50 files showed only 21.

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

int main(void)
{
    // TODO
    unsigned char original1[512] = {0xff, 0xd8, 0xff, 0xe0};
    unsigned char original2[512] = {0xff, 0xd8, 0xff, 0xe1};
    unsigned char buffer[512];

    FILE* outfile;

    FILE* memory = fopen("card.raw", "r");
    if (memory == NULL)
    {
       return 1;
    }

    int count = 0;
    int match = 0;
    int check = 0;
    char name[8];

    while(!feof(memory))
    {
        fread(&buffer, 512, 1, memory);

        for(int i = 0; i < 4; i++)
        {
            if(original1[i] == buffer[i] || original2[i] == buffer[i])
            {
                match++;
            }
        }

        if(match == 4)
        {
            if(check == 1)
            {
                fclose(outfile);
            }

            sprintf(name, "%.3d.jpeg", count);
            outfile = fopen(name, "w");
            if(outfile == NULL)
            {
                return 2;
            }
            count++;

            fwrite(&buffer, 512, 1, outfile);
            check = 1;
        }
        else if(check == 1)
        {
            fwrite(&buffer, 512, 1, outfile);
        }
        else
        {
            check = 0;
        }
    }

    fclose(memory);
    fclose(outfile);
    return 0;
}
1

It's amazing how often one small oversight can trash an entire program! Welcome to the wonderful world of "Oops, I missed something small!" We all get this, more often than we care to admit! ;-)

The good news is that your program logic works. In your program, the code checks for the signature with a for loop that counts the number of matching bytes and then checks to see if it is 4. However, after the first signature block is found, match is never reset to 0. That means that the next block, and every block after that leaves match at 4, until the code sees that the first byte in a 512 byte block matches a signature. From then on, no more files are created. Also, all of the files that were created are all invalid. The first one was only 1 block in size, and every file after that is created from a block that is somewhere inside of an image file. Did you notice that they were all 512 byte files?

The code needs to reset match before executing the for loop.

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

1
  • lol, now i get what did you mean with you first words.thank you ,i did what you said and it's working. – Ahmed Rifaat Dec 11 '16 at 7:40

You must log in to answer this question.

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