使用fscanf()在C中用文件信息填充链表,它';What’没有读取文件的第一行吗?

使用fscanf()在C中用文件信息填充链表,它';What’没有读取文件的第一行吗?,c,io,linked-list,scanf,C,Io,Linked List,Scanf,我试图用长度为“未知”的文件input.txt中的信息填充一个链接列表。使用fscanf()扫描信息,并根据“name”变量按字母顺序插入到链接列表中 由于某些原因,fscanf()将不会读取和提取输入文件的第一行。在本例中,该行为“Selena Boeing Management 5 30”。这也恰好是按字母顺序排列时被推到列表末尾的元素 输入文件:(姓名、公司、部门、工作年限、年龄) 下面是我关注的代码,特别是用于填充列表的while循环: #include <stdlib.h>

我试图用长度为“未知”的文件input.txt中的信息填充一个链接列表。使用fscanf()扫描信息,并根据“name”变量按字母顺序插入到链接列表中

由于某些原因,fscanf()将不会读取和提取输入文件的第一行。在本例中,该行为“Selena Boeing Management 5 30”。这也恰好是按字母顺序排列时被推到列表末尾的元素

输入文件:(姓名、公司、部门、工作年限、年龄)

下面是我关注的代码,特别是用于填充列表的while循环:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define NAME_LENGTH 20
#define COMPANY_LENGTH 20
#define DIVISION_LENGTH 15

//Create employee Structure
typedef struct employee{
        char* name;
        char* company;
        char* division;
        int yearsEmployed;
        int age;
        struct employee *next;
}employee;

//Declare Function Prototypes
employee* new_employee(char*, char*, char*, int, int);
employee* insert_by_employee(employee*, employee*);
void print_list(employee*);


int main(int argc, char** argv) {

    FILE *in;
    char* name = (char*)malloc(sizeof(char*) * NAME_LENGTH);
    char* company = (char*)malloc(sizeof(char*) * COMPANY_LENGTH);
    char* division = (char*)malloc(sizeof(char*) * DIVISION_LENGTH);

    int yearsEmployed = 0;
    int age = 0;

    //1. --------------Error Checking-----------------
    if(argc < 2) //Check the number of arguments.
    {
            printf("You have entered the incorrect number of command line arguments.\n");
            printf("Program failed to start. Exiting. . .\n");
            return 1; //Exit Program
    }

    if((in = fopen("input.txt", "r")) == NULL) //Did the file successfully open?
    {
            printf("The input file failed to open.\n");
            printf("Program cannot continue. Exiting. . .\n");
            return 1; //Exit Program
    }


    //2. ------Linked List Operations------
    employee* head = NULL; //Create Empty Linked List
    employee* current = NULL;



    while(!feof(in)) //Check for file end
    {
             //Read first data value to kickstart.
            if(fscanf(in, "%s %s %s %d %d", name, company, division, &yearsEmployed, &age) == EOF) {
                    break;
            }

            employee* hold = new_employee(name, company, division, yearsEmployed, age);
            head = insert_by_employee(head, hold);


    }

    //3. ------Print the new List------
    print_list(head);
    return 1; //Exit Success
}
赛琳娜的信息去哪了

编辑:以下是打印列表功能:

void print_list(employee* head)
{
    employee* current;
    current = head;
    char p[] = "Employee";
    char c[] = "Company";
    char t[] = "Division";
    char k[] = "YrsEmployed";
    char d[] = "Age";
    //Header
    printf("\n\n|%10s | %10s | %10s | %10s | %10s|\n", p, c, t, k, d);
    printf("-----------------------------------------------------------------------\n");


    while(current->next != NULL)
    {
            printf("|%10s | %10s | %10s | %10d | %10d|\n", current->name, current->company, current->division, current->yearsEmployed, current->age);
            current = current->next;
    }
    printf("-----------------------------------------------------------------------\n");

    return;
}

你的打印功能坏了。
打印列表中的循环条件应为:

while(current != NULL)

否则,将永远不会打印最后一个元素。Selena在那里,你只是没有打印它。

你能不能也发布你的
打印列表(head)
?事实上Selena是列表中最后一个,这意味着在循环中逐个错误检查你的终止条件。@Jerry\u Y打印列表(head)已经添加,谢谢。@Mike这也是我的想法,但是,即使我进行循环并手动创建条件,以便它精确循环有多少个元素,它也只会重复另一个元素,说“Bob的”信息两次。它总是漏掉最后一个节点(按字母顺序排列)。这肯定是问题所在,我仍然掌握着链表的窍门。我会一直这样,我很感激!
|      Name |    Company |    Division | YrsEmployed|        Age|
-----------------------------------------------------------------------
|       Bob |     Oracle | Engineering |         20 |         55|
|      Gary |     Google |       Sales |          6 |         40|
|      Jill |  Microsoft |     Finance |          4 |         28|
|      Mary |     Garmin |     Finance |         10 |         35|
|      Mike |      Apple |   Marketing |          3 |         38|
-----------------------------------------------------------------------
void print_list(employee* head)
{
    employee* current;
    current = head;
    char p[] = "Employee";
    char c[] = "Company";
    char t[] = "Division";
    char k[] = "YrsEmployed";
    char d[] = "Age";
    //Header
    printf("\n\n|%10s | %10s | %10s | %10s | %10s|\n", p, c, t, k, d);
    printf("-----------------------------------------------------------------------\n");


    while(current->next != NULL)
    {
            printf("|%10s | %10s | %10s | %10d | %10d|\n", current->name, current->company, current->division, current->yearsEmployed, current->age);
            current = current->next;
    }
    printf("-----------------------------------------------------------------------\n");

    return;
}
while(current != NULL)