将文本文件读入单个链接列表。并打印列表

时间:2015-04-17 18:12:16

标签: c linked-list

您好我正在为大学做项目,在项目中我必须使用txt文件中的员工数据填充链接列表。当我读入文件时,它会部分填充链表。当我打印节点时,它只打印第一组员工数据。 txt文件中有18个员工详细信息。所以应该打印18个节点。我不知道为什么它不会,任何帮助非常感谢。 (我已经在头文件中全局声明了我的listHead指针。) 见下面的代码

      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;
    }
}

2 个答案:

答案 0 :(得分:1)

您需要解决多个问题。我会强调这些问题:

1)在填写条目之前,您需要为每个列表项分配(malloc())。在您的情况下,您只是阅读每个值并将其放入相同的条目中。你可以看到你的while循环中没有malloc。

2)你在开始时为temp分配空间然后扔掉那个记忆:

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

第一行为联系人分配空间,第二行将temp重新分配给为头部分配的空间。您刚刚分配的空间现在没有指向它的指针,永远无法再次访问它。

3)fflush(stdin)应该做什么。我认为应该删除。

答案 1 :(得分:1)

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

内存泄漏,您覆盖malloc

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

错误,请参阅Why is “while ( !feof (file) )” always wrong?

使用

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

您需要为文件的每一行预留新的contact空间(使用malloc)(这就是为什么它只打印第一组员工数据)