gpt4 book ai didi

c - 读取纺织品并添加到 C 中的链接列表

转载 作者:行者123 更新时间:2023-11-30 17:15:52 25 4
gpt4 key购买 nike

我正在尝试在此程序中创建一个 createLinkedListNode 函数。在此函数中,我打开一个作为参数的文本文件,并向用户询问员工结构中的信息,然后从文件指针引用的文件中逐行读取它。我将数据添加到链接列表中。我初始化链表的结构部分并将下一个指针设置为 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 之后打印,您可以计算行并填充您的结构,您必须将字符串转换为整数/ double 。

编辑:这里是如何填充结构的剧透,这不是理想的解决方案,因为它应该更强大,例如处理文件与您期望的不同的情况,但它会可能对你有帮助。

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

    char *fName[25];
char *lName[35];

这将为您创建一个 25/35 char* 的数组,我认为您希望没有 *,所以它只是一个 25/35 char* 的数组(这是一个 char*)

剧透!!!所以我在这里写了一个小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;
}

最后一件事,如果没有特殊原因您用大 L 命名该位置,您应该将其称为位置

如果有什么问题请告诉我^^

关于c - 读取纺织品并添加到 C 中的链接列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29838727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com