0

I have a segmentation fault that won't go away. Code:

/**

[mod edit to hide code behind a link]

Running gdb, after a few lines, I get an error saying "iofopen.c: No such file or directory." Why is this happening, because the problem doesn't exist on resize.c?

EDIT: I tried some revisions, some suggested (thanks), and many to try to get rid of the fault, but it is still there, in the same place. New code:

[mod snip out irrelevant code]

            char* outName = "";
            sprintf(outName, "0%i%i.jpg", tens, ones);

1 Answer 1

1

It's segfaulting on your fclose(outptr); because your outptr is null, probably, given that you have never actually opened one.

Remember that the bytes aren't of type char. They are an 8 bit unsigned type (an unsigned char) so your array should be created with that type. Also, atoi() is not the correct function here, as that will always return 0. Simply compare the byte to its appropriate hex value, like if(buffer[0] == 0xff) etc.

Also, sizeof(inptr) is the size of the pointer (i.e., 4 bytes) so your loop will only run 4 times. This is another reason why you never found a jpg.


edit

This:

char* outName = "";
sprintf(outName, "0%i%i.jpg", tens, ones);

You are declaring a string literal when you set a char* to a value using "". By definition, this variable is fixed and trying to write to it will result in a segfault.

Change that to a char array rather than a char* (making sure to make the array long enough to hold all the chars in your outName including the nul-terminator.)

Also, how are you determining the size of the card.raw file? You cannot hardcode its size into your program as there is no guarantee that you will be given the exact same one another time. You need to figure out how to determine if you've read everything. Perhaps consider using the return value of your fread() as your loop condition.

3
  • I implementer your suggestion, and made some other changes, but the segfault is still there. Revised code is in the post. Commented Aug 11, 2015 at 20:03
  • Thanks. I even modified the code so the size of card.raw is no longer hardcoded. Commented Aug 12, 2015 at 1:41
  • So is it working now? (i.e., no longer segfaulting?) if so, please accept my answer to remove your question from the unanswered queue. Feel free to open a new question if you still have other issues. Thanks.
    – curiouskiwi
    Commented Aug 12, 2015 at 6:00

You must log in to answer this question.

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