0

My program runs properly through to the end of file but only recovers 13 images. Based on the fact that the jpegs are back to back and that my program reaches the end of the card.raw file, I'm assuming I must be missing the initial 3 photos. I've been staring at my code and using gdb for the past few days and cannot figure out what could possibly be wrong with it. Any help would be appreciated.

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

#include <stdint.h>

typedef uint8_t BYTE;

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

// ensure proper usage
if (argc != 1)
{
    printf("Command line not recognized.\n");
    return 1;
}

// open raw file
FILE* inptr = fopen("card.raw", "r");
if (inptr == NULL)
{
    printf("card.raw file not available\n");
    return 2;
}

// format filename for 00n notation
char filename[8];
BYTE store[512];

// read until jpg starts
while (store[0] != 0xff || store[1] != 0xd8 || store[2] != 0xff || store[3] != (0xe0 & 0xe1))
{
    fread(store, 512, 1, inptr);
}

// start loop to make a new file for each image
for (int i = 0; i < 16; i++)
{
    // store file name as a string to format properly
    sprintf(filename, "%03d.jpg", i);

    // create output files
    FILE* outptr = fopen(filename, "w");
    if (outptr == NULL)
    {
        fclose(inptr);
        printf("File could not be created\n");
        return 3;
    }

    // write the first line of the jpeg and move to the next block
    fwrite(store, 512, 1, outptr);
    if (feof(inptr))
    {
        break;
    }
    fread(store, 512, 1, inptr);
    if (feof(inptr))
    {
        fwrite(store, 512, 1, outptr);
        break;
    }

    // continue writing file until next jpeg is reached
    while (store[0] != 0xff || store[1] != 0xd8 || store[2] != 0xff || store[3] != (0xe0 & 0xe1))
    {
        fwrite(store, 512, 1, outptr);
        fread(store, 512, 1, inptr);
        if (feof(inptr))
        {
            fwrite(store, 512, 1, outptr);
            break;   
        }
    } 
    fclose(outptr);
    if (feof(inptr))
    {
        break;
    }
}
fclose(inptr);
return 0;

}

0

Look at your test for the jpeg signatures, specifically store[3] != (0xe0 & 0xe1)). Your intent is to see if either of these values is in store[3]. The problem is that the "&" is actually the bitwise AND operator. The parentheses tell the computer to do the bitwise AND before doing the test.

This code is actually doing a bitwise AND between 0xe0 and 0xe1 before the comparison. The result is 0xe0, which you are then comparing to store[3]. It is just lucky that the result of the comparison is one of the two values you are checking for. I would wager that the three files that are missing have the 0xe1 signature and could be spread anywhere in the series.

What you want is to make two separate checks, something like

... (store[3] != 0xe0 && store[3] != 0xe1)

(assuming I have the logic right.)

If this answers your question, please mark it as answered. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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