为什么保存的txt会乱码,而且保存函数有警告,怎么修改
#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;
}
_save(head);
}
// 创建文件
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();break;
case 4:update();break;
case 5:displayEmployees();break;
case 6:displayStatistics();break;
case 7:exit(0);
default:printf("无效的选择!\n");
}
}
return 0;
} 保存的txt文件乱码的问题是因为在保存函数`_save`中,使用了二进制写入方式(`wb`),这样会将结构体直接按二进制形式写入文件,导致文件内容呈现乱码。另外,在读取字符串时,应该使用`%s`而不是`%c`。
要解决保存文件乱码的问题,可以使用文本写入方式(`w`)将结构体成员逐个写入文件中。此外,在读取字符串时应该用`%s`。
修改后的`_save`函数如下:
// 保存员工信息到文件
void _save(Employee* head) {
if (head == NULL) {
printf("链表为空,保存失败\n");
return;
}
FILE* fp = fopen("employee_list.txt", "w");
if (fp == NULL) {
printf("打开文件失败\n");
return;
}
Employee* p = head;
while (p != NULL) {
fprintf(fp, "%d %s %.2f %.2f\n", p->id, p->name, p->salary, p->tax);
p = p->next;
}
fclose(fp);
printf("员工信息已保存到文件中\n");
}
修改完保存函数之后,还需要修改其他地方读取文件的方式。以下为修改之后的完整代码:
#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", "w");
if (fp == NULL) {
printf("打开文件失败\n");
return;
}
Employee* p = head;
while (p != NULL) {
fprintf(fp, "%d %s %.2f %.2f\n", p->id, p->name, p->salary, p->tax);
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 = (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 <= 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);
}
// 根据姓名查询员工信息
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("%s", 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\n", 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("管理员登录成功!\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]