为什么没有创建文件后没有保存数据到文件里面呢
#include <stdio.h>#include <stdlib.h>
#include <string.h>
#include <windows.h>
typedef struct Employee{
int id;
char password;
char name;
float salary;
float tax;
struct Employee*next;
}Employee;
// 全局变量,用于存储员工链表的头指针
Employee*head=NULL;
// 管理员登录函数
int adminLogin()
{
char password;
printf("请输入管理员密码:");
scanf("%s",password);
if(strcmp(password,"2597658468")==0)
{
return 1;
}
else
{
return 0;
}
}
// 录入员工信息并计算税金
void addEmployee()
{
int i,m;
printf("职工人数(1--100):");
scanf("%d",&m);
for(i=0;i<m;i++)
{
Employee*newEmployee =(Employee*)malloc(sizeof(Employee));
printf("请输入员工工号:");
scanf("%d",&newEmployee->id);
printf("请输入员工密码:");
scanf("%s",newEmployee->password);
printf("请输入员工姓名:");
scanf("%s",&newEmployee->name);
printf("请输入员工工资:");
scanf("%f",&newEmployee->salary);
if(newEmployee->salary<=3000)
{
newEmployee->tax=newEmployee->salary*0.03;
}
else if(newEmployee->salary>3000&&newEmployee->salary<=12000)
{
newEmployee->tax=(newEmployee->salary-3000)*0.10;
}
else if(newEmployee->salary>12000&&newEmployee->salary<=25000)
{
newEmployee->tax=(newEmployee->salary-12000)*0.20;
}
else if(newEmployee->salary>25000&&newEmployee->salary<=35000)
{
newEmployee->tax=(newEmployee->salary-25000)*0.25;
}
else if(newEmployee->salary>35000&&newEmployee->salary<=55000)
{
newEmployee->tax=(newEmployee->salary-35000)*0.30;
}
else if(newEmployee->salary>55000&&newEmployee->salary<=80000)
{
newEmployee->tax=(newEmployee->salary-55000)*0.35;
}
else
{
newEmployee->tax=(newEmployee->salary-80000)*0.45;
}
newEmployee->next=head;
head=newEmployee;
}
}
void creat()
{
FILE *fp;
// 使用读入方式打开文件
fp=fopen("employee_list.txt","r");
if(fp==NULL)
{
fp=fopen("employee_list.txt","w");
}
fclose(fp);
}
void save(Employee* head)
{
if(head==NULL)
{
printf("链表为空,保存失败\n");
return;
}
FILE* fp=fopen("employee_list.txt","wb");
if(fp==NULL)
{
printf("打开文件失败\n");
return;
}
Employee* p = head;
while(p!=NULL)
{
fwrite(p, sizeof(Employee), 1, fp);
p = p->next;
}
fclose(fp);
printf("员工信息已保存到文件中\n");
}
// 根据姓名查询员工信息
void serchEmployees()
{
int flag=1;
char name;
Employee* p=head;
printf("请输入你要查找的姓名: \n");
scanf("%s",name);
while(p!=NULL&&flag)
{
if(strcmp(name,p->name)==0)
{
flag=0;
}
else
{
p=p->next;
}
}
if(flag==0)
{
printf("工号\t姓名\t工资\n");
printf("%d\t",p->id);
printf("%s\t",p->name);
printf("%.2f\t",p->salary);
}
else
{
printf("没有查询到记录");
}
}
//根据姓名进行修改
void update() {
char name; // 姓名字符串
printf("请输入要修改信息的学生姓名:\n");
scanf("%s", name);
Employee *current = head; // 当前节点指针
// 遍历链表,查找匹配姓名的学生节点
while (current != NULL) {
if (strcmp(current->name, name) == 0) {
printf("请输入新的工号:\n");
scanf("%d", &(current->id));
printf("请输入新的密码:\n");
scanf("%s", current->password);
printf("请输入修改的名字:\n");
scanf("%d", &(current->name));
printf("请输入修改的工资:\n");
scanf("%f", &(current->salary));
printf("修改成功!\n");
return; // 修改成功后直接返回
}
current = current->next;
}
printf("未找到该学生信息!\n");
}
// 显示所有员工的信息
void displayEmployees()
{
Employee* current =head;
while(current !=NULL)
{
printf("工号:%d\t姓名: %s\t工资: %.2f\t税金: %.2f\t",current->id,current->name,current->salary,current->tax);
current=current->next;
}
}
// 统计数目
void displayStatistics()
{
int count=0;
float totalSalary =0;
float totalTax =0;
Employee* current =head;
while(current!=NULL)
{
count++;
totalSalary+=current->salary;
totalTax+=current->tax;
current=current->next;
}
printf("员工数目:%d,工资总数: %.2f,税金总数:%.2f\n",count,totalSalary,totalTax);
}
// 主函数
int main()
{
int choice;
creat();
while(1)
{
printf("\n1.管理员登录\n2.录入员工信息\n3.查找员工信息\n4.修改员工信息\n5.显示所有员工信息\n6.显示统计信息\n7.退出\n");
printf("请输入你的选择:");
scanf("%d",&choice);
switch(choice)
{
case 1:
if(adminLogin())
{
printf("管理员登录成功!");
}
else
{
printf("管理员密码错误!");
}
break;
case 2:addEmployee();break;
case 3:serchEmployees();save(head);break;
case 4:update();break;
case 5:displayEmployees();break;
case 6:displayStatistics();break;
case 7:exit(0);
default:printf("无效的选择!\n");
}
}
return 0;
} 在你的代码中,没有将数据保存到文件的原因是因为你在调用`save()`函数之前并没有创建员工链表。
在`addEmployee()`函数里,你只是创建了一个新的员工节点并将其添加到链表的头部,但是你没有将链表头指针`head`重新赋值给新节点的地址。所以,即使你成功创建了员工节点,但是你的链表头指针还是指向NULL,所以链表为空。
解决这个问题的方法是,在`addEmployee()`函数结束之后,将链表头指针`head`重新赋值为新创建的节点的地址。
另外,你在`adminLogin()`函数中,将管理员密码定义为一个字符变量`password`,但是你却用`%s`格式化字符串来读取密码,这是错误的,应该使用`%c`来读取单个字符。
下面是修改后的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Employee {
int id;
char password;
char name;
float salary;
float tax;
struct Employee* next;
} Employee;
// 全局变量,用于存储员工链表的头指针
Employee* head = NULL;
// 创建文件
void creat() {
FILE* fp;
// 使用读入方式打开文件
fp = fopen("employee_list.txt", "r");
if (fp == NULL) {
fp = fopen("employee_list.txt", "w");
fclose(fp);
}
}
// 保存员工信息到文件
void save(Employee* head) {
if (head == NULL) {
printf("链表为空,保存失败\n");
return;
}
FILE* fp = fopen("employee_list.txt", "wb");
if (fp == NULL) {
printf("打开文件失败\n");
return;
}
Employee* p = head;
while (p != NULL) {
fwrite(p, sizeof(Employee), 1, fp);
p = p->next;
}
fclose(fp);
printf("员工信息已保存到文件中\n");
}
// 录入员工信息并计算税金
void addEmployee() {
int i, m;
printf("请输入职工人数(1--100):");
scanf("%d", &m);
for (i = 0; i < m; i++) {
Employee* newEmployee = malloc(sizeof(Employee));
printf("请输入员工ID:");
scanf("%d", &newEmployee->id);
printf("请输入员工密码:");
scanf(" %c", &newEmployee->password);
printf("请输入员工姓名:");
scanf(" %c", &newEmployee->name);
printf("请输入员工工资:");
scanf("%f", &newEmployee->salary);
if (newEmployee->salary <= 3000) {
newEmployee->tax = newEmployee->salary * 0.03;
} else if (newEmployee->salary <= 12000) {
newEmployee->tax = (newEmployee->salary - 3000) * 0.10;
} else if (newEmployee->salary <= 25000) {
newEmployee->tax = (newEmployee->salary - 12000) * 0.20;
} else if (newEmployee->salary <= 35000) {
newEmployee->tax = (newEmployee->salary - 25000) * 0.25;
} else if (newEmployee->salary <= 55000) {
newEmployee->tax = (newEmployee->salary - 35000) * 0.30;
} else {
newEmployee->tax = (newEmployee->salary - 55000) * 0.35;
}
newEmployee->next = head;
head = newEmployee;
}
// 将链表头指针保存到文件
save(head);
}
int main() {
int choice;
creat();
while (1) {
printf("\n1.管理员登录\n2.录入员工信息\n3.查找员工信息\n4.修改员工信息\n5.显示所有员工信息\n6.显示统计信息\n7.退出\n");
printf("请输入你的选择:");
scanf("%d", &choice);
switch (choice) {
case 1:
// 管理员登录
if (adminLogin()) {
printf("管理员登录成功!\n");
} else {
printf("管理员密码错误!\n");
}
break;
case 2:
// 录入员工信息
addEmployee();
break;
case 3:
// 查找员工信息
serchEmployees();
break;
case 4:
// 修改员工信息
update();
break;
case 5:
// 显示所有员工信息
displayEmployees();
break;
case 6:
// 显示统计信息
displayStatistics();
break;
case 7:
// 退出程序
exit(0);
default:
printf("无效的选择!\n");
}
}
return 0;
}
希望对你有帮助!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页:
[1]