鱼C论坛

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

怎么修改

[复制链接]
发表于 2024-1-7 19:02:02 | 显示全部楼层 |阅读模式

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

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

x
  1. void deleteEmployee() {
  2.     int id;
  3.     printf("请输入要删除的员工工号:");
  4.     scanf("%d", &id);

  5.     Employee* current = head;
  6.     Employee* prev = NULL;

  7.     while (current != NULL && current->id != id) {
  8.         prev = current;
  9.         current = current->next;
  10.     }

  11.     if (current == NULL) {
  12.         printf("未找到该员工信息!\n");
  13.         return;
  14.     }

  15.     if (prev == NULL) {
  16.         head = current->next;
  17.     } else {
  18.         prev->next = current->next;
  19.     }

  20.     free(current);
  21.     printf("员工信息删除成功!\n");

  22.     _save(head);
  23. }
复制代码

怎么修改是保存在文件里面的内容也删除呢
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-1-7 19:02:23 | 显示全部楼层
要实现将内容保存在文件中,并删除文件中的相应内容,可以按照以下步骤进行修改:

1. 在删除员工信息之前,先读取保存在文件中的所有员工信息,并将其存储在链表中。

2. 修改代码中的`deleteEmployee()`函数,添加一个新的局部变量`Employee* temp`,用于遍历链表。

3. 在遍历链表并找到要删除的员工信息后,将`current`指针指向的员工节点从链表中删除。

4. 使用`_save()`函数将更新后的链表重新保存到文件中。

下面是修改后的代码示例:

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. typedef struct employee {
  4.     int id;
  5.     struct employee* next;
  6. } Employee;

  7. Employee* head = NULL;

  8. void _save(Employee* list) {
  9.     FILE* file = fopen("employees.txt", "w");
  10.     if (file == NULL) {
  11.         printf("无法打开文件!\n");
  12.         return;
  13.     }

  14.     Employee* current = list;
  15.     while (current != NULL) {
  16.         fprintf(file, "%d\n", current->id);
  17.         current = current->next;
  18.     }

  19.     fclose(file);
  20. }

  21. void load() {
  22.     FILE* file = fopen("employees.txt", "r");
  23.     if (file == NULL) {
  24.         printf("无法打开文件!\n");
  25.         return;
  26.     }

  27.     int id;
  28.     while (fscanf(file, "%d", &id) != EOF) {
  29.         Employee* newEmployee = (Employee*)malloc(sizeof(Employee));
  30.         newEmployee->id = id;
  31.         newEmployee->next = NULL;

  32.         if (head == NULL) {
  33.             head = newEmployee;
  34.         } else {
  35.             Employee* current = head;
  36.             while (current->next != NULL) {
  37.                 current = current->next;
  38.             }
  39.             current->next = newEmployee;
  40.         }
  41.     }

  42.     fclose(file);
  43. }

  44. void deleteEmployee() {
  45.     int id;
  46.     printf("请输入要删除的员工工号:");
  47.     scanf("%d", &id);

  48.     load();  // 读取保存的员工信息

  49.     Employee* current = head;
  50.     Employee* prev = NULL;
  51.     Employee* temp = NULL;

  52.     while (current != NULL) {
  53.         if (current->id == id) {
  54.             if (prev == NULL) {
  55.                 head = current->next;
  56.             } else {
  57.                 prev->next = current->next;
  58.             }
  59.             temp = current;
  60.             current = current->next;
  61.             free(temp);  // 释放删除的节点
  62.             printf("员工信息删除成功!\n");
  63.             break;
  64.         }
  65.         prev = current;
  66.         current = current->next;
  67.     }

  68.     _save(head);  // 将更新后的链表保存到文件中
  69. }

  70. int main() {
  71.     deleteEmployee();
  72.     return 0;
  73. }
复制代码


在这个示例中,我们新增了一个`load()`函数,用于从文件中加载保存的员工信息,并将其存储在链表中。然后,在删除员工信息之前,调用`load()`函数加载文件中的数据。删除操作完成后,再调用`_save()`函数将更新后的链表保存回文件中。

这样,你就可以实现将内容保存在文件中,并删除文件中对应的员工信息了。

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-4 15:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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