gpt4 book ai didi

c - 从文件读取到 LinkedList C 时,最终字段为 NULL

转载 作者:太空宇宙 更新时间:2023-11-03 23:47:14 26 4
gpt4 key购买 nike

所以我正在从一个文件读入一个链表。一切正常,除了最后一个字段“email”,它只是读取 null。有人有什么想法吗?

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


#define ID_LENGTH 25
#define NAME_LENGTH 25
#define address_LENGTH 25
#define department_LENGTH 25
#define joinDate_LENGTH 25
#define email_LENGTH 30

//Create employee Structure
typedef struct employee{
char* ID;
char* name;
char* address;
char* department;
char* joinDate;
double salary;
char* email;
struct employee *next;
}employee;

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


int main() {

FILE *in;
char* ID = (char*)malloc(sizeof(char*) * ID_LENGTH);
char* name = (char*)malloc(sizeof(char*) * NAME_LENGTH);
char* department = (char*)malloc(sizeof(char*) * department_LENGTH);
char* address = (char*)malloc(sizeof(char*) * address_LENGTH);
char* joinDate = (char*)malloc(sizeof(char*) * joinDate_LENGTH);
double salary = 0;
char* email = (char*)malloc(sizeof(char*) * email_LENGTH);


if ((in = fopen("Employees.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 %s %s %d %s", ID, name, department, address, joinDate, &salary, email) == EOF) {
break;
}

employee* hold = new_employee(ID, name, department, address, joinDate, salary, email);
head = insert_by_employee(head, hold);


}

//3. ------Print the new List------
print_list(head);
system("pause");
return 1; //Exit Success
}
employee* new_employee(char* id, char* name, char* department, char* address, char* joinDate, double salary, char* email) {

//Create new employee and malloc space
employee* new = (employee*)malloc(sizeof(struct employee));
new->ID = (char*)malloc(sizeof(char) * ID_LENGTH);
new->name = (char*)malloc(sizeof(char) * NAME_LENGTH);
new->department = (char*)malloc(sizeof(char) * department_LENGTH);
new->address = (char*)malloc(sizeof(char) * address_LENGTH);
new->joinDate = (char*)malloc(sizeof(char) * joinDate_LENGTH);
new->email = (char*)malloc(sizeof(char) * email_LENGTH);



//Set data
strcpy(new->ID, id);
strcpy(new->name, name);
strcpy(new->department, department);
strcpy(new->address, address);
strcpy(new->joinDate, joinDate);
new->salary = salary;
strcpy(new->email, email);
//Retun a pointer to the node
return new;

}

//Inserts new node into an alphabetically sorted linked list.
employee* insert_by_employee(employee* head, employee* new)
{
employee* current = NULL;
current = head;
if (current == NULL || strcmp(current->name, new->name) > 0)
{
new->next = current;
return new;
}
else {

while (current->next != NULL && strcmp(current->next->name, new->name) < 0)
{

current = current->next;
}
}
new->next = current->next;
current->next = new;
return head;

}
void print_list(employee* head)
{
employee* current;
current = head;
char i[] = "Employee ID";
char n[] = "Name";
char a[] = "Address";
char d[] = "Department";
char jd[] = "Join Date";
char s[] = "Salary";
char em[] = "Email";


//Header
printf("\n\n|%15s | %15s | %15s | %15s | %15s| %15s | %15s|\n", i, n, a, d, jd, s, em);
printf("--------------------------------------------------------------------------------------------------------------------------------------------\n");


while (current != NULL)
{
printf("|%15s | %15s | %15s |%15s | %15s | %15d| %15s\n", current->ID, current->name, current->address, current->department, current->joinDate, current->salary, current->email);
current = current->next;
}
printf("-------------------------------------------------------------------------------------------------------------------------------------------\n");


return;
}

这是将数据输入链表的示例文件

EMP1 Martin Clare Computer 06/06/1996 60000 hannanmartin@gmail.com
EMP2 John Galway Accounting 05/05/1995 50000 johnemp@gmail.com
EMP3 Kevin Kilkenny Computer 04/04/1994 34000 kevinemp@gmail.com
EMP4 Kim Sligo Sales 03/03/1993 65000 kimemp@gmail.com
EMP5 Keith Ballina Accounting 02/02/1992 53200 keithemp@gmail.com
EMP6 Tim Dublin Sales 01/01/1991 23000 timemp@gmail.com
EMP7 Andy Chicago Computer 07/07/1990 10000 andyemp@gmail.com

最佳答案

薪水是 double 而不是整数。但是您在 scanf 和 printf 调用中一直使用 %d。将 %lf 用于 double ,或将 salary 更改为 int。

关于c - 从文件读取到 LinkedList C 时,最终字段为 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29908166/

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