As was foretold, we've added advertisements to the forums! If you have questions, or if you encounter any bugs, please visit this thread: https://forums.penny-arcade.com/discussion/240191/forum-advertisement-faq-and-reports-thread/

C and Segmentation Faults (strcmp/fclose)

SeguerSeguer of the VoidSydney, AustraliaRegistered User regular
edited November 2007 in Help / Advice Forum
Hey guys,

In desperation turn we to H/A!

I have a C assignment (not going to get you guys to do it for me :P) that is causing me headaches; I'm using fgets to get lines from a file (which works fine), but if I try to strcmp the line that results from that, I get a segmentation fault. Unless I printf the line first. Then, upon trying to close the file using fclose I get another fault, even after adding an if statement to check that the file pointer is not null!

Help me, H/A!

Seguer on

Posts

  • falsedeffalsedef Registered User regular
    edited November 2007
    Need to see some code. How are you allocating the cstrings? You probably failed to allocate space correctly.

    falsedef on
  • SeguerSeguer of the Void Sydney, AustraliaRegistered User regular
    edited November 2007
    FILE* packets = openFile(file); //openFile just uses fopen and checks for NULL etc
    char line[MAX_CHARS_TO_READ]; //space of 100
    fgets(line, MAX_CHARS_TO_READ, packets)


    EDIT:

    strcmp stuff:

    char *messageID;
    messageID = strtok(cp, ":");

    if (strcmp(messageID, "END\n"))

    {

    return 1;

    }




    EDIT: SOLVED It was strcpy causing it. Argh.

    Seguer on
  • theSquidtheSquid Sydney, AustraliaRegistered User regular
    edited November 2007
    Isn't it safer to use strncmp instead of strcmp? It won't solve the problem, but it's good practice (prevents buffer overflows, which *may* be the problem) and it might at least prevent the segfault.

    EDIT: As well, strncpy.

    theSquid on
  • SeguerSeguer of the Void Sydney, AustraliaRegistered User regular
    edited November 2007
    Alright well rather than start a new thread:

    Does anyone know how to declare a pointer to a pointer to a struct, so that I can edit the values in the struct? I have plenty of line* start working, but how do I do that with line** start, etc?

    Seguer on
  • CKyleCKyle Registered User regular
    edited November 2007
    struct myStructure **p;
    // assign stuff

    (*p)->field = value;
    // or (**p).field = value;

    Did that help?

    CKyle on
  • SeguerSeguer of the Void Sydney, AustraliaRegistered User regular
    edited November 2007
    I'll have to try it later, but how would I then edit the values in (**p)?

    Seguer on
  • SmasherSmasher Starting to get dizzy Registered User regular
    edited November 2007
    I think there's some sort of misunderstanding going on. Why do you need a pointer to a pointer to a struct to change the values of the variables inside it?

    There are times when pointers to pointers to things are appropriate (multidimensional arrays, for example), but in this case I suspect you're unintentionally overcomplicating things. What exactly are you attempting to do?

    Smasher on
Sign In or Register to comment.