0

I red a lot of segmentation fault questions/answers but It seems that I have the main one ok.... Please help me out to find out my error :

    //define data structure for having exactly 1 byte
    typedef uint8_t BYTE;


    //to store bytes of the size of an array = 512 =block size long  (size of an array of 512 which is 1 byte long) => store memory which bunch of 512 bytes called buffer
    BYTE buffer[512];

    //count for number of JPEG file - should get total of 50 with raw
    int count = 0;
    FILE *image = NULL;

    //read the memory card until end of file (end of memory card) so no more 512 block left, where size of block is 512 from FAT
    while (fread(buffer, 512, 1, cardfile) == 1)
    {
        //check the first 4 bytes pattern to find the JPEG signature (fourth byte 0Xe (0 à 7 ou a à f))
        if (buffer [0] == 0xff && buffer [1] == 0xd8 && buffer [2] == 0xff && (buffer [3] == 0xe0 || buffer[3] == 0xe1))
        {

            //if an outfile in open, close it to than start a new file
            if (image != NULL);
            {
                fclose(image);
            }


            //if JPEG signature start a new file so create the file to be opened to be able to copy the JPEG image from memory card

            //create name of the file with char for jpeg filames with 3 digits => 000.jpg (7 caracters) + "\ 0" (+1 caracter) = total of 8caracters
            char filename [8];
            //create a name for each file
            sprintf(filename,"%03i.jpg", count);
            //increase number of filename for next jpg found => 000.jpg, 001.jpg. 002.jpg etc...
            count ++;

            //open the file
            image = fopen(filename, "a");
            //output file
            if (image == NULL);
            {
                fprintf(stderr, "Could not open new file %s\n", filename);
                return 3;
            }

            //write the new file so copy the memory card ofr one image at a time
            fwrite(buffer, 512, 1, image);

        }
        // write image by image // write the 0 as 0 if blank space
        //if ( size != 1) //stop reading because it s
        // fin c'est le début d'un autre JPEG

        //if no JPEG signature, just copy on the file until closed or end of file (when found a new signature file will be closed)
        else if (count > 0)
        {
            //write all the blocks after the JPEG signature
            fwrite(buffer, 512, 1, image);
        }
    }

    // close outfile each one at the time ( when encounter another signature)
    fclose(image);
    // close memory file
    fclose(cardfile);


    // success
    return 0;
}
1

I found it. in the loop, when i found a signature JPEG, I have to close the image only if count > 0 and not if image == NULL !

//read the memory card until end of file (end of memory card) so no more 512 block left, where size of block is 512 from FAT while (fread(buffer, 512, 1, cardfile) == 1) { //check the first 4 bytes pattern to find the JPEG signature (fourth byte 0Xe (0 à 7 ou a à f)) if (buffer [0] == 0xff && buffer [1] == 0xd8 && buffer [2] == 0xff && (buffer [3] == 0xe0 || buffer[3] == 0xe1)) {

        //if an outfile is open, close it to than start a new file
        if (count > 0)
        {
            fclose(image);
        }
3
  • For above code, both image != NULL and count > 0 should be equivalent. !=, not ==. – Blauelf Aug 24 '17 at 5:42
  • so image == NULL is not equivalent to image != NULL ? – Genevie Aug 24 '17 at 21:09
  • == means equals, != means doesn't equal. They are opposites. Whenever one tests to true, the other one tests to false. – Blauelf Aug 25 '17 at 10:03
0

You could use debug50 to step through your code and find the line causing the segmentation fault. Like debug50 ./recover

One obvious case where a segmentation fault might happen is the final fclose(image), this should be protected by some if (image != NULL) or if (count > 0).

File access mode "a" means "append", you should use "w" for "write".

3
  • well I swithc for w and I add up the protection for the last fclose(image) but stil have the same error but my debug50 don t stay more thatn 2 sec on the screen :( – Genevie Aug 23 '17 at 23:49
  • Is that an answer? – toyotasupra Sep 10 '17 at 10:12
  • @Genevie debug50 should be used with breakpoints (click left of line number in editor). – Blauelf Sep 11 '17 at 5:30

You must log in to answer this question.

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