在C语言中读取纺织品并添加到链表中

在C语言中读取纺织品并添加到链表中,c,pointers,file-io,struct,linked-list,C,Pointers,File Io,Struct,Linked List,我正在尝试在此程序中创建createLinkedListNode函数。在这个函数中,我打开一个作为参数的文本文件,向用户询问employee结构中的信息,然后从文件指针中引用的文件逐行读取。我将数据添加到链接列表中。我初始化链表的struct部分,将下一个指针设置为NULL,并返回指向这个新创建的节点的指针。我有两个结构,这让我很困惑,因为我不知道如何将数据添加到指定的结构: struct List{ struct EMPLOYEE{ int ID; ch

我正在尝试在此程序中创建createLinkedListNode函数。在这个函数中,我打开一个作为参数的文本文件,向用户询问employee结构中的信息,然后从文件指针中引用的文件逐行读取。我将数据添加到链接列表中。我初始化链表的struct部分,将下一个指针设置为NULL,并返回指向这个新创建的节点的指针。我有两个结构,这让我很困惑,因为我不知道如何将数据添加到指定的结构:

struct List{
    struct EMPLOYEE{
        int ID;
        char *fName[25];
        char *lName[35];
        double salary;
        char Location;
    }employee;
    struct List *next;
};
这是我有两个链表的主要函数,我还没有初始化它们。但我在这里调用createLinkedList函数,并调用文件指针:

void main() {
    FILE *fp;
    struct List *onHead = NULL;
    struct List *offHead = NULL;

    createLinkedListNode();
}
当一行一行地读取文本文件并相应地添加数据时,我就不知所措了。如果有人能做到,那就太好了

struct List *createLinkedListNode(FILE *fp, struct List *list, int ID, char fName, char lName, double salary, char Location){
    //This is the created node with its memory allocated.
    struct List* node = (struct List)*malloc(sizeof(struct List));

    fp =fopen("textfile.txt","r");

    //prompt for all info required in passenger struct
    printf("Enter Passenger ID:\n");
    fgets()
    node ->passenger.ID = ID;
    printf("Enter first name:\n");
    node ->passenger.fName = fName;
    printf("Enter last name:\n");
    node ->passenger.lName = lName;
    printf("Enter salary:\n");
    node ->passenger.CBalance= CBalance;

    printf("Enter location:\n");
    printf("1- on\n");
    printf("0- off\n");
    node ->passenger.Location =Location;

    node ->next = list;
    return node;
}
假设正在阅读的纺织品是这样的:

0001
John
Tyler 
23.00
1
0002
Erin
Marc
25.00
0
0003
Jason
Ed
15.00
1

我不太了解您的问题,但关于fgets的一些信息似乎可以帮助您^^^让您了解fgets的工作原理:

char buffer[100];
FILE *fp = fopen("textfile.txt","r");

while (fgets(buffer, 100, fp)) {
    printf("%s", buffer);
}
这将逐行打印整个文件。对fgets的每次调用都会将文件的下一行存储在第一个参数中传递的字符串中(第二个参数是您可以读取的最大字符量,最后一个参数是您的文件)

当到达文件末尾时,fgets返回null,这就是while的工作方式

因此,不必在每个fgets之后打印,您可以计算行并填充结构,而需要将字符串转换为整数/双tho

编辑:下面是如何填充结构的破坏者,它不是理想的解决方案,因为它应该更健壮,就像处理文件与您期望的不一样的情况一样,但它可能会帮助您

我还认为你的结构有问题,你写道:

    char *fName[25];
    char *lName[35];
这将创建一个25/35字符的数组*,我想您希望它没有*所以它只是一个25/35字符的数组(这是一个字符*)

剧透所以我在这里写了一个小C文件,它读取你的文本文件并打印列表以显示它的工作原理,正如我所说的,它必须改进以用于一个严肃的软件,例如,文件可能不是我们想要的

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

struct List{
    struct EMPLOYEE{
        int ID;
        char fName[25];
        char lName[35];
        double salary;
        char location;
    }employee;
    struct List *next;
};

int main(int argc, char const *argv[])
{
    char buffer[100];
    struct List* node = NULL;

    FILE *fp = fopen("textfile.txt","r");

    printf("Reading file....\n");

    while (fgets(buffer, 100, fp)) {
        // File is not empty, it should at least contain a full employee
        struct List* new_node = malloc(sizeof(struct List));
        new_node->next = NULL;

        // ID
        new_node->employee.ID = atoi(buffer);

        // fName
        fgets(buffer, 25, fp);
        int l = strlen(buffer);
        if (l > 0 && buffer[l-1] == '\n') buffer[l-1] = '\0'; // We dont want the \n in the name ^^
        strcpy(new_node->employee.fName, buffer);

        // lName
        fgets(buffer, 35, fp);
        l = strlen(buffer);
        if (l > 0 && buffer[l-1] == '\n') buffer[l-1] = '\0'; // We dont want the \n in the name ^^
        strcpy(new_node->employee.lName, buffer);

        // salary
        fgets(buffer, 100, fp);
        new_node->employee.salary = atof(buffer);

        // Location
        fgets(buffer, 100, fp);
        new_node->employee.location = buffer[0];

        // We add to the list
        if (node == NULL) {
            node = new_node;
        } else {
            new_node->next = node;
            node = new_node;
        }
    }

    printf("We're done\n");

    while (node != NULL) {
        printf("Employee:\n");
        printf("  ID:\t\t%i\n", node->employee.ID);
        printf("  fName:\t%s\n", node->employee.fName);
        printf("  lName:\t%s\n", node->employee.lName);
        printf("  Salary:\t%f\n", node->employee.salary);
        printf("  location:\t%c\n\n", node->employee.location);
        node = node->next;
    }

    // Memory leaks fest :)
    return 0;
}
#包括
#包括
结构列表{
结构雇员{
int-ID;
char fName[25];
字符名称[35];
双薪;
字符位置;
}员工;
结构列表*下一步;
};
int main(int argc,char const*argv[]
{
字符缓冲区[100];
结构列表*节点=NULL;
FILE*fp=fopen(“textfile.txt”、“r”);
printf(“读取文件…”\n);
而(fgets(缓冲区,100,fp)){
//文件不是空的,它至少应该包含一个完整的员工
结构列表*新节点=malloc(sizeof(结构列表));
新建节点->下一步=空;
//身份证
新建_节点->employee.ID=atoi(缓冲区);
//fName
fgets(缓冲区,25,fp);
int l=strlen(缓冲区);
如果(l>0&&buffer[l-1]='\n')buffer[l-1]='\0';//我们不希望名称中有\n^^
strcpy(新建节点->employee.fName,缓冲区);
//名字
fgets(缓冲区,35,fp);
l=strlen(缓冲器);
如果(l>0&&buffer[l-1]='\n')buffer[l-1]='\0';//我们不希望名称中有\n^^
strcpy(新建节点->employee.lName,缓冲区);
//薪水
fgets(缓冲区,100,fp);
新建_节点->employee.salary=atof(缓冲区);
//位置
fgets(缓冲区,100,fp);
新建_节点->employee.location=buffer[0];
//我们把它添加到列表中
if(node==NULL){
节点=新节点;
}否则{
新建节点->下一步=节点;
节点=新节点;
}
}
printf(“我们完成了”\n);
while(节点!=NULL){
printf(“员工:\n”);
printf(“ID:\t\t%i\n”,节点->employee.ID);
printf(“fName:\t%s\n”,节点->employee.fName);
printf(“lName:\t%s\n”,节点->employee.lName);
printf(“工资:\t%f\n”,节点->员工工资);
printf(“位置:\t%c\n\n”,节点->员工位置);
节点=节点->下一步;
}
//内存泄漏fest:)
返回0;
}
还有最后一件事,如果没有特别的原因,你用一个大L来命名这个位置,你应该称它为location


告诉我是否出了什么问题^ ^

你的问题有点让人困惑。1.您的意思是从文件中读取学生数据还是提示用户输入?你似乎同时暗示了两者。2.这两个链表应该代表什么?3.对
createLinkedListNode
的调用与定义不匹配(缺少参数且忽略了返回值)。4.你的具体问题是什么?您需要比“它不工作”更具体。1)数据只从文件中读取,而不是从用户中读取。问题就在那里,这样你就可以看到每一行被单独阅读。2) 稍后将使用这两个链接列表,如果员工位置为1,他将添加到在职人员列表,如果他们的位置为0,他将添加到离职人员列表。3) 我不知道我在函数中的参数是否正确,所以我暂时将其留空。4) 我的具体问题是如何从文件中读取此数据,并将其添加到结构和链表中,然后创建此节点,以便稍后使用。