当前位置: 首页 > news >正文

C语言实战2

C语言实战案例:文件操作与数据结构

案例目标:
实现一个学生信息管理系统,包含文件读写、链表操作和基本增删改查功能。

文件读写与链表结合

定义学生结构体并创建链表:

typedef struct Student { int id; char name[50]; float score; struct Student *next; } Student;

文件写入函数示例:

void saveToFile(Student *head, const char *filename) { FILE *file = fopen(filename, "w"); if (!file) return; Student *current = head; while (current != NULL) { fprintf(file, "%d %s %.2f\n", current->id, current->name, current->score); current = current->next; } fclose(file); }

动态内存管理

从文件加载数据到链表:

Student* loadFromFile(const char *filename) { FILE *file = fopen(filename, "r"); if (!file) return NULL; Student *head = NULL, *tail = NULL; while (!feof(file)) { Student *newNode = (Student*)malloc(sizeof(Student)); if (fscanf(file, "%d %s %f", &newNode->id, newNode->name, &newNode->score) == 3) { newNode->next = NULL; if (!head) head = newNode; else tail->next = newNode; tail = newNode; } else { free(newNode); break; } } fclose(file); return head; }

实用功能实现

链表插入与删除操作:

void insertStudent(Student **head, int id, const char *name, float score) { Student *newNode = (Student*)malloc(sizeof(Student)); newNode->id = id; strcpy(newNode->name, name); newNode->score = score; newNode->next = *head; *head = newNode; } void deleteStudent(Student **head, int id) { Student *temp = *head, *prev = NULL; while (temp != NULL && temp->id != id) { prev = temp; temp = temp->next; } if (temp == NULL) return; if (prev == NULL) *head = temp->next; else prev->next = temp->next; free(temp); }

高级应用示例

排序功能实现(冒泡排序):

void sortByScore(Student **head) { if (!*head || !(*head)->next) return; int swapped; Student *ptr1; Student *lptr = NULL; do { swapped = 0; ptr1 = *head; while (ptr1->next != lptr) { if (ptr1->score < ptr1->next->score) { // 交换数据域 Student temp = *ptr1; ptr1->id = ptr1->next->id; strcpy(ptr1->name, ptr1->next->name); ptr1->score = ptr1->next->score; ptr1->next->id = temp.id; strcpy(ptr1->next->name, temp.name); ptr1->next->score = temp.score; swapped = 1; } ptr1 = ptr1->next; } lptr = ptr1; } while (swapped); }

错误处理增强

增加文件操作安全检查:

Student* safeLoad(const char *filename) { FILE *file = fopen(filename, "r"); if (!file) { perror("Error opening file"); return NULL; } // ...(其余加载逻辑) if (ferror(file)) { perror("Error reading file"); clearerr(file); fclose(file); return NULL; } fclose(file); return head; }

该案例完整实现了文件持久化存储、动态内存管理、链表操作等核心功能,可作为C语言中级练习的典型范例。实际开发时可进一步扩展搜索功能、界面交互等模块。

http://www.cnnetsun.cn/news/16285.html

相关文章:

  • 分布式应用框架Microsoft Orleans - 2、动手实践:构建你的第一个Microsoft Orleans应用程序
  • MAC电脑如何开发淘晶驰串口屏
  • Java 中 new 一个对象的过程是怎么样的?
  • 从规则引擎到大模型:文档生成技术的十年进化与现在的最佳实践
  • AI客户端终极指南:多平台支持与工作区管理快速上手
  • 安全审查--跨站请求伪造--Fetch Metadata防护模式
  • uni-app x封装request,统一API接口请求
  • 4大维度解析DeepLX与官方API:技术实战与成本效益终极评测
  • 本地 AI 服务难共享?TRAE SOLO+cpolar 轻松打破局域网枷锁
  • 助力金融信创与云原生转型,DeepFlow 排障智能体和可观测性建设实践
  • 靠谱的模板网站建设哪家好
  • PuLID技术深度解析:重新定义人物身份定制的新范式
  • SGLang结构化生成语言:重塑大模型工具调用的新范式
  • Windows Insider免登录终极指南:轻松获取预览版更新
  • FluidNC运动控制固件:重新定义ESP32 CNC设备的智能控制
  • 【光照】Unity[PBR]环境光中的[漫反射]
  • 39、NFS与网络路由管理:配置、问题诊断及参数调优
  • CentOS7 磁盘扩容
  • PDFMathTranslate中文乱码终极解决方案:从诊断到完美修复
  • 直接数字下变频 原理解释和python仿真
  • 告别低效内耗:2025中小企业办公新方式
  • 微信7.0.6提示升级问题解决方法
  • 大模型训练优化:5个内存效率提升技巧与实战配置指南
  • 英伟达发布OpenReasoning-Nemotron-32B:多智能体协作改写推理范式,32B参数刷新三大领域性能纪录
  • Lottie-Web实战指南:打造高性能动画应用
  • 思源宋体实战指南:从零到精通的字体应用全解析
  • 转载Centos7.9 MySQL 8.0 部署MGR高可用
  • Spring管理MyBatis Mapper接口的原理详解
  • ISO 19011-2018管理体系审核指南中文版资源详解
  • 第十届网络安全与信息工程国际会议(ICCSIE 2025)已被EI检索