0

I'm pretty new to C programming and I am trying to build a console application that takes in some user survey data into a singly linked list and generates statistics out of it.

The main idea is that every time application user opens up the application, the existing survey data is loaded from text file into linked list nodes. User can then manipulate data as they want.

My issue is that application crashes every time the importList function is called and I can't figure out where my issue is. I would be really grateful if anyone can point out where my mistake is. Thank you.

Here is the function for importing data from text file:

void importList(struct nodeList** head)
{

    //set up the linked list node by node
    struct nodeList *temp;
    temp =(struct nodeList*)malloc(sizeof(struct nodeList));
    temp = *head;

    //open survey data file in read mode to import existing survey data
    openSurveyFile();

    //read in data that's in the text file to nodes in linked list
    while(!feof(surFPtr))
    {
        //import data from file. All data except address is held in single word lines.
        fscanf(surFPtr, "%d", &temp->PPS);
        fscanf(surFPtr, "%s", temp->name);
        fscanf(surFPtr, "%s", temp->surname);
        fgets(temp->address, 50, surFPtr);//since address is a multiword string the list needs to read the entire line
        fscanf(surFPtr, "%s", temp->eMail);
        fscanf(surFPtr, "%d", &temp->age);
        fscanf(surFPtr, "%d", &temp->income);
        fscanf(surFPtr, "%s", temp->gender);
        fscanf(surFPtr, "%d", &temp->exercise);
        fscanf(surFPtr, "%d", &temp->alcohol);
        fscanf(surFPtr, "%d", &temp->smoking);
        fflush(stdin);

        //allocate memory for the next node in list
        temp->next =(struct nodeList*)malloc(sizeof(struct nodeList));
        temp = temp->next;//move on to next node
    }
    //close survey data file once all data is imported
    closeSurveyFile();
}//end of importList function
Bentoy13
  • 4,886
  • 1
  • 20
  • 33
M.V.
  • 1
  • 1
  • 1
    Well, did you step through the code in a debugger? Please show definition of nodeList. – OldProgrammer Mar 30 '16 at 12:00
  • 1
    1) there is no reason for this function to return void. 2) `while (!feof()) {}` is always wrong. 3) you are malloccing an element before you kwow that you'll need it. 4) don't cast the return value from malloc() 5) check the return values of fscanf() and fgets() – wildplasser Mar 30 '16 at 12:08
  • [Read this first](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). [Read this next](http://stackoverflow.com/questions/5810410/alternative-for-fflushstdin). [Furthermore read this](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – n. m. could be an AI Mar 30 '16 at 12:09

1 Answers1

0

There is an error near the beginning: you are losing the memory allocated first into temp since you are replacing temp with *head. If *head is null when you call this function, you get an exception when you dereference the pointer. Maybe you want to do *head = temp; instead, to get the head of your list back?

Bentoy13
  • 4,886
  • 1
  • 20
  • 33
  • In addition to this, I strongly advise to apply the comments of n.m. and wildpasser, there are good pratices to make a safe code. – Bentoy13 Mar 30 '16 at 15:01