0

I rearranged my code several times. Also searched and saw so many people’s codes with segmentation fault and changed the code according to advice they got but my code keeps giving me the same mistake. When I run debug50 it creates an image. Please, help. https://pastebin.com/WQ7HiGSM

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


int main(int argc, char *argv[])
{
    // ensure proper usage
    if (argc != 2)
    {
        fprintf(stderr, "Usage: one command-line argument (argc)\n");
        return 1;
    }
    char *infile = argv[1];
    //blocks of the memory
    uint8_t buffer[512] = {0};
    //track new images

    int nimage = 0;
    //open card
    FILE *inptr = fopen(infile, "r");
    if (inptr == NULL)
    {
        fprintf(stderr, "Could not open %s.\n", infile);
        return 1;
    }
    char* outfile = NULL;
    FILE *outptr = fopen(outfile, "w");

    //reading bytes
    while(fread(buffer, sizeof(buffer), 1, inptr) == 1)
    {
        //loking for the first three bytes of the beggining of jpg
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        {
            //read file shoud be inside of condition
            if(outfile != NULL)
            {
                fclose(outptr);
            }
            nimage++;
            char image[8];
            sprintf(image, "%.3d.jpg", nimage);
            outptr = fopen(image, "w");
            if(outfile == NULL)
            {
                fclose(inptr);
            }

            fwrite(buffer, sizeof(buffer), 1, outptr);
        }
        else if(outfile != NULL)
        {
            fwrite(buffer, sizeof(buffer), 1, outptr);
        }
    }
    // close infile
    fclose(inptr);

    // close outfile
    fclose(outptr);

    // success
    return 0;
}
0

The segmentation fault probably happens at

    char* outfile = NULL;
    FILE *outptr = fopen(outfile, "w");

The variable you pass for the file name is a NULL pointer, if fopen dereferences that (and I assume it does), this will segfault. Poor Binky.

Before you enter the main loop, you don't even know there will be any JPEG in the card image. So I wouldn't open an output file before you need it.

Also, outfile serves no real purpose. You use it to detect whether you've already found a file, but you could use outptr for that purpose (initialised to NULL).

If you close inptr as a reaction to an error, print something and return from main, otherwise the programme will continue and try to write to a non-existent file.

The format string for sprintf seems wrong, or at least it's not what I would expect, not sure what it does, I don't think . is allowed in a %d type format. You should add 0 for padding.

Also, you might need to change where you increment the file number.

PS: Bugs that change on trying to observe them are also called Heisenbugs. This might be due to different memory layout, different timing, a slightly different environment in the debugger.

You must log in to answer this question.

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