我正在尝试在这个程序中创建一个createLinkedListNode函数。在这个函数中,我打开一个文本文件,它是一个参数,我向用户请求employee结构中的信息,然后从file指针中引用的文件中逐行读取它。我将数据添加到链表中。初始化链表的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;
}假设正在读取的textile是这样的:
0001
John
Tyler
23.00
1
0002
Erin
Marc
25.00
0
0003
Jason
Ed
15.00
1发布于 2015-04-24 15:46:58
我真的不太理解你的问题,但看起来关于fgets的一些信息会对你有帮助^^,让你看看fgets是如何工作的:
char buffer[100];
FILE *fp = fopen("textfile.txt","r");
while (fgets(buffer, 100, fp)) {
printf("%s", buffer);
}这将逐行打印整个文件。每次调用fgets都会将文件的下一行存储在传递给第一个参数的字符串中(第二个参数是可以读取的最大字符量,最后一个是文件)
当你到达文件的末尾时,fgets返回null,这就是while的工作方式。
所以不是在每次fget之后打印,你可以计算行并填充你的struct,你将不得不将字符串转换成整数/双精度tho。
编辑:这里有一些关于如何填充你的结构的剧透,它不是理想的解决方案,因为它应该更健壮,就像处理文件不像你期望的那样,但它可能会对你有所帮助。
另外,我认为你的结构有问题,你可以这样写:
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,则应将其命名为location
告诉我有什么问题^^
https://stackoverflow.com/questions/29838727
复制相似问题