鱼C论坛

 找回密码
 立即注册
查看: 1330|回复: 1

修改姓名成功后保存到txt里面的信息怎么修改呢

[复制链接]
发表于 2023-12-27 13:35:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. void update() {
  2.     char name[50]; // 姓名字符串
  3.     printf("请输入要修改信息的学生姓名:\n");
  4.     scanf("%s", name);
  5.     Employee *current = head; // 当前节点指针
  6.     // 遍历链表,查找匹配姓名的学生节点
  7.     while (current != NULL) {
  8.         if (strcmp(current->name, name) == 0) {
  9.             printf("请输入新的工号:\n");
  10.             scanf("%d", &(current->id));
  11.             printf("请输入新的密码:\n");
  12.             scanf("%s", current->password);
  13.             printf("请输入修改的名字:\n");
  14.             scanf("%s", current->name);
  15.             printf("请输入修改的工资:\n");
  16.             scanf("%f", &(current->salary));
  17.             printf("修改成功!\n");
  18.             return; // 修改成功后直接返回
  19.         }
  20.         current = current->next;
  21.     }
  22.       
  23.     printf("未找到该学生信息!\n");
  24. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-12-27 13:36:06 | 显示全部楼层
要将修改后的信息保存到txt文件中,可以按照以下步骤进行操作:
1. 在update()函数前声明一个全局变量,用于存储txt文件的路径:
  1. char *filename = "employee.txt";
复制代码
2. 在update()函数中,找到匹配姓名的学生节点后,将新的信息写入txt文件中:
  1. FILE *file = fopen(filename, "w");
  2. if (file == NULL) {
  3.     printf("文件打开失败!\n");
  4.     return;
  5. }
  6. current = head;
  7. while (current != NULL) {
  8.     fprintf(file, "%d\t%s\t%s\t%f\n", current->id, current->name, current->password, current->salary);
  9.     current = current->next;
  10. }
  11. fclose(file);
  12. printf("修改成功!\n");
  13. return;
复制代码
3. 这样,每次修改成功后,都会将链表中的所有信息重新写入txt文件中,从而实现保存修改后的信息。注意,这种方法会覆盖原有的txt文件内容。

完整代码如下所示:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. typedef struct Employee {
  5.     int id; // 工号
  6.     char name[100]; // 姓名
  7.     char password[100]; // 密码
  8.     float salary; // 工资
  9.     struct Employee *next; // 下一个节点指针
  10. } Employee;
  11. Employee *head = NULL;
  12. void update() {
  13.     char name[100]; // 姓名字符串
  14.     printf("请输入要修改信息的学生姓名:\n");
  15.     scanf("%s", name);
  16.     Employee *current = head; // 当前节点指针
  17.     // 遍历链表,查找匹配姓名的学生节点
  18.     while (current != NULL) {
  19.         if (strcmp(current->name, name) == 0) {
  20.             printf("请输入新的工号:\n");
  21.             scanf("%d", &(current->id));
  22.             printf("请输入新的密码:\n");
  23.             scanf("%s", current->password);
  24.             printf("请输入修改的名字:\n");
  25.             scanf("%s", current->name);
  26.             printf("请输入修改的工资:\n");
  27.             scanf("%f", &(current->salary));
  28.             
  29.             FILE *file = fopen("employee.txt", "w");
  30.             if (file == NULL) {
  31.                 printf("文件打开失败!\n");
  32.                 return;
  33.             }
  34.             Employee *tmp = head;
  35.             while (tmp != NULL) {
  36.                 fprintf(file, "%d\t%s\t%s\t%f\n", tmp->id, tmp->name, tmp->password, tmp->salary);
  37.                 tmp = tmp->next;
  38.             }
  39.             
  40.             fclose(file);
  41.             printf("修改成功!\n");
  42.             return; // 修改成功后直接返回
  43.         }
  44.         current = current->next;
  45.     }
  46.       
  47.     printf("未找到该学生信息!\n");
  48. }
  49. int main() {
  50.     // 读取txt文件中的信息到链表中
  51.     FILE *file = fopen("employee.txt", "r");
  52.     if (file == NULL) {
  53.         printf("文件打开失败!\n");
  54.         return 0;
  55.     }
  56.     Employee *current = NULL;
  57.     while (!feof(file)) {
  58.         Employee *node = (Employee *)malloc(sizeof(Employee));
  59.         fscanf(file, "%d\t%s\t%s\t%f\n", &(node->id), node->name, node->password, &(node->salary));
  60.         node->next = NULL;
  61.         
  62.         if (head == NULL) {
  63.             head = node;
  64.         } else {
  65.             current->next = node;
  66.         }
  67.         current = node;
  68.     }
  69.     fclose(file);
  70.     update();
  71.     // 释放链表内存
  72.     current = head;
  73.     while (current != NULL) {
  74.         Employee *tmp = current;
  75.         current = current->next;
  76.         free(tmp);
  77.     }
  78.     return 0;
  79. }
复制代码

请注意,上述代码仅演示了如何将修改后的信息写入txt文件中,并没有考虑到输入信息时的错误处理和链表的其他操作。在实际应用中,还需要根据具体需求进行补充和修改。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-17 08:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表