1

Hi I'm doing a project for college,in the project I have to populated the linked list with data about employees from a txt file. When I read the file in, it partly populates the linked list. And when I print the nodes it only prints the first set of employee data. There are 18 employee details on the txt file. So there should be 18 nodes printed. I'm At a loss to why it wont, any help greatly appreciated. (I have declared my listHead pointer globally in a header file.) see code below

      struct contact{
        int employeeId;
        char firstName[15];
        char lastName[15];
        char employeeAddress[40];
        char email[25];
        char department[25];
        float annualSalary;
        struct date
        {
            int day;
            int month;
            int year;
        }doj;
        struct contact *next;

   };

//linked list
struct contact *listHead;
// initializes the list with a head 
void initLinkList(){
// set head
listHead = (struct contact *)malloc(sizeof(struct contact));
listHead->next = NULL;
} // initLinkList

void main()
{
    initLinkList();
    struct contact *temp;
    temp = (struct contact*)malloc(sizeof(struct contact));
    temp = listHead;
    FILE *cfPtr;
    if ((cfPtr = fopen(FILENAME, READMODE)) == NULL){
    puts("File could not be opened");
    }
    else{
        fscanf(cfPtr, "%d %s %s %s %s %s %f %d %d %d",
            &temp->employeeId,
            &temp->firstName,
            &temp->lastName,
            &temp->employeeAddress,
            &temp->email,
            &temp->department,
            &temp->annualSalary,
            &temp->doj.day,
            &temp->doj.month,
            &temp->doj.year);

         while (feof == 0)
        {
           fscanf(cfPtr, "%d %s %s %s %s %s %f %d %d %d",
                &temp->employeeId,
                &temp->firstName,
                &temp->lastName,
                &temp->employeeAddress,
                &temp->email,
                &temp->department,
                &temp->annualSalary,
                &temp->doj.day,
                &temp->doj.month,
                &temp->doj.year);
                fflush(stdin);

             temp->next = temp;
            //listHead->next = temp;
            printf("HELP");
        }
       fclose(cfPtr);
    }
do{
    menu();
    switch (userChoice){
    case 1:
        add();
        userChoice = NULL;
        break;
    case 2:
        printNodes();
        userChoice = NULL;
        break;
    case 3:
        view();
        userChoice = NULL;
        break;
    }
} while (userChoice != -1);

printf("\n\n\n");
system("pause");
}
void printNodes()
{
    struct contact *temp;
    temp = (struct contact*)malloc(sizeof(struct contact));
    temp = listHead ;
    while (temp != NULL)
    {
        printf("\n\nEmployee id: %d", temp->employeeId); // show the data
        printf("\n\nEmployee First Name: %s", temp->firstName);
        printf("\n\nEmployee Last Name: %s", temp->lastName);
        printf("\n\nEmployee Adress: %s", temp->employeeAddress);
        printf("\n\nEmployee Email: %s", temp->email);
        printf("\n\nEmployee Department: %s", temp->department);
        printf("\n\nEmployee Start Date");
        printf("\n\n-------------------");
        printf("\n\nDay: %d", temp->doj.day);
        printf("\n\nMonth: %d", temp->doj.month);
        printf("\n\nYear: %d", temp->doj.year);
        temp = temp->next;
    }
}
DevRight
  • 345
  • 1
  • 6
  • 25
  • 2
    Run your code through a debugger. Track the list building to ensure it is being done right. See where things go awry. – Politank-Z Apr 17 '15 at 18:16
  • 1
    There are several things wrong with your program. The main one is that you keep reading into the same temp overwriting the previously read data. You need to be mallocing a new one on each loop. – Tony Lee Apr 17 '15 at 18:19
  • A detail - in `printNodes()` the line `temp = (struct contact*)malloc(sizeof(struct contact));` is redundant, since you assign the list's head pointer to `temp` immediately after (and you didn't `free(temp)` - anyway you *can't* because you have lost the value returned by `malloc`). You do the same thing in `main()` - allocate memory to `temp` then over write the pointer. – Weather Vane Apr 17 '15 at 18:23
  • A simple list does not need a `struct` for its head - just a pointer, initially `NULL`. Then for each new node `*temp` (that you must `malloc`) you can do `temp->next = listHead; listHead = temp;` but **not** `temp->next = temp;` – Weather Vane Apr 17 '15 at 18:34

2 Answers2

1

There are multiple problems that you need to address. I'll highlight those problems:

1) You need to allocate (malloc()) for each list item before filling the entries. In your case you are just reading every value and putting it into the same entry. You can see that your while loop doesn't have a malloc in it.

2) You allocate space for temp in the beginning and then you throw that memory away:

temp = (struct contact*)malloc(sizeof(struct contact));
temp = listHead;

The first line allocates space for a contact and the second line reassigns temp to the space allocated for the head. The space that you just allocated now doesn't have a pointer to it and it can never be accessed again.

3) What is the fflush(stdin) supposed to do. I believe that should just be deleted.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
Brad Budlong
  • 1,775
  • 11
  • 10
1
temp = (struct contact*)malloc(sizeof(struct contact));
temp = listHead;

Memory leak, you overwrite the result of malloc


    while (feof == 0) /* you mean while (feof(cfPtr) != 0) */
    {
       fscanf(cfPtr, "%d %s %s %s %s %s %f %d %d %d",

wrong, see Why is “while ( !feof (file) )” always wrong?

Use

while (fscanf(cfPtr, "%d %s %s %s %s %s %f %d %d %d", ...) == 10) {...}

And you need to reserve space for a new contact (using malloc) for each line of the file (this is why it only prints the first set of employee data)

Community
  • 1
  • 1
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • This worked for me in the end. I was not initializing the head node properly, so it printed junk values also. But it works now. Thanks – DevRight Apr 21 '15 at 16:36