news 2026/4/23 18:52:38

C语言实战2

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
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语言中级练习的典型范例。实际开发时可进一步扩展搜索功能、界面交互等模块。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/23 9:18:21

如何在5分钟内快速部署SendPortal邮件营销平台

如何在5分钟内快速部署SendPortal邮件营销平台 【免费下载链接】sendportal Open-source self-hosted email marketing. Manage your own newsletters at a fraction of the cost. 项目地址: https://gitcode.com/gh_mirrors/se/sendportal SendPortal是一款现代化的开…

作者头像 李华
网站建设 2026/4/23 10:47:41

计算机毕业设计springboot大连科技学院运动会管理系统 SpringBoot高校田径运动会综合管理平台基于 SpringBoot的校园体育赛事智能编排与成绩发布系统

计算机毕业设计springboot大连科技学院运动会管理系统w3b6ki7s &#xff08;配套有源码 程序 mysql数据库 论文&#xff09; 本套源码可以在文本联xi,先看具体系统功能演示视频领取&#xff0c;可分享源码参考。高校运动会历来是校园体育文化的集中展示&#xff0c;但传统人工排…

作者头像 李华
网站建设 2026/4/23 12:10:33

TradingAgents-CN智能配置:从零构建AI金融交易系统的实战指南

引言&#xff1a;当传统投资遇见AI智能体 【免费下载链接】TradingAgents-CN 基于多智能体LLM的中文金融交易框架 - TradingAgents中文增强版 项目地址: https://gitcode.com/GitHub_Trending/tr/TradingAgents-CN 你是否曾经面临这样的困境&#xff1a;面对海量的市场数…

作者头像 李华
网站建设 2026/4/23 4:35:56

博客主题定制终极指南:3步打造专业级个人博客

博客主题定制终极指南&#xff1a;3步打造专业级个人博客 【免费下载链接】hexo-theme-next Elegant theme for Hexo. 项目地址: https://gitcode.com/gh_mirrors/he/hexo-theme-next 想要一个既美观又独特的个人博客&#xff0c;却不知道从何开始&#xff1f;本文将为…

作者头像 李华
网站建设 2026/4/23 12:10:19

【实操指南】通过YIBUAPI快速接入GTP-5.2,零基础也能上手的AI集成教程

在人工智能技术飞速发展的当下&#xff0c;越来越多的开发者和企业希望快速接入优质大模型&#xff0c;赋能创新应用开发。近期&#xff0c;OpenAI重磅推出的GTP-5.2凭借超强的职场实用能力、低幻觉率及出色的长文本处理能力引发行业热潮。笔者发现一款高效的大模型接入平台——…

作者头像 李华