0

Hope you got into the new year well and healthy! I get a segmentation fault (core dumped) if I want to run ./recover card.raw Anyone got any tips because I have checked it 100 times already. Valgrind has detected no errors, either.

Thanks!

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

    typedef uint8_t BYTE;

    int main(int argc, char *argv[])
    {
// Check usage, if not 1 command line argument, deliver error message
if (argc != 2)
{
    printf("Fail! Usage: recover image\n");
    return 1;
}

// Open file and check if file is not null
FILE *cardraw = fopen(argv[1], "r");

if (cardraw == NULL)
{
    return 1;
}

BYTE *bytes= malloc(512);
int counter = 0;
char filename [8];
FILE *img = NULL;

// Copy card.raw to image, one BYTE at a time
// Read first three bytes
//Read 512bytes into buffer

while (fread(bytes, 1, 512, cardraw) != 0x00)
{
    // Check first 4 bytes
    if (bytes[0] == 0xff && bytes[1] == 0xd8 && bytes[2] == 0xff && (bytes[3] & 0xf0) == 0xe0)

        {   //If first jpeg
            if(counter == 0)

            {   //Create new jpeg file and name
                sprintf(filename, "%03i.jpg", counter);
                img = fopen(filename, "w");
                fwrite(bytes, 1, 512, img);
                counter++;
            }

            else  
            {   //Close current img file
                fclose(img);

                //Open new img file
                sprintf(filename, "%03i.jpg", counter);
                img = fopen(filename, "w");
                fwrite(bytes, 1, 512, img);
                counter++;
            }
        }
    else
        {
            //If already found JPEG continue writing
            fwrite(bytes, 1, 512, img);
        }

}
// Close file, free memory
fclose(cardraw);
fclose(img);
free(bytes);
return 0;

}

0

Question: What happens if the very first 512 byte data block does not contain a signature???? ;-)

Debugging tips: This might help you out a lot -

Do YOU know how to find a seg fault?? Advice to new programmers

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

1

You must log in to answer this question.

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