开启你的编程学习之旅

云课堂提供高质量的编程课程,从入门到精通,助你成为技术大牛

立即开始学习

C语言程序开发

作者: 夏老师 更新: 2025-10-20 阅读: 4432 难度: 中级
学习工具

5. C语言结构体和文件操作

结构体用于组织不同类型的数据,文件操作用于数据的持久化存储。

结构体定义和使用

结构体示例
#include <stdio.h> #include <string.h> // 定义结构体 struct Student { int id; char name[50]; int age; float score; }; // 定义包含结构体的结构体 struct Class { int classId; char className[20]; struct Student students[50]; int studentCount; }; // 函数声明 void printStudent(struct Student s); void inputStudent(struct Student *s); void printClass(struct Class c); int main() { // 结构体变量声明和初始化 struct Student student1 = {1001, "张三", 20, 85.5}; struct Student student2; // 逐个赋值 student2.id = 1002; strcpy(student2.name, "李四"); student2.age = 21; student2.score = 92.0; printf("学生1信息:\n"); printStudent(student1); printf("\n学生2信息:\n"); printStudent(student2); // 结构体指针 struct Student *ptr = &student1; printf("\n通过指针访问学生1姓名: %s\n", ptr->name); // 输入学生信息 struct Student student3; printf("\n请输入学生3信息:\n"); inputStudent(&student3); printf("\n输入的学生3信息:\n"); printStudent(student3); // 结构体数组 struct Student students[3] = { {2001, "王五", 19, 78.5}, {2002, "赵六", 20, 88.0}, {2003, "钱七", 21, 95.5} }; printf("\n=== 学生数组 ===\n"); for (int i = 0; i < 3; i++) { printStudent(students[i]); } // 嵌套结构体 struct Class class1 = {1, "计算机1班", {0}, 0}; // 添加学生到班级 class1.students[0] = student1; class1.students[1] = student2; class1.studentCount = 2; printf("\n=== 班级信息 ===\n"); printClass(class1); return 0; } // 打印学生信息函数 void printStudent(struct Student s) { printf("学号: %d\n", s.id); printf("姓名: %s\n", s.name); printf("年龄: %d\n", s.age); printf("成绩: %.1f\n", s.score); } // 输入学生信息函数 void inputStudent(struct Student *s) { printf("请输入学号: "); scanf("%d", &s->id); printf("请输入姓名: "); scanf("%s", s->name); printf("请输入年龄: "); scanf("%d", &s->age); printf("请输入成绩: "); scanf("%f", &s->score); } // 打印班级信息函数 void printClass(struct Class c) { printf("班级ID: %d\n", c.classId); printf("班级名称: %s\n", c.className); printf("学生数量: %d\n", c.studentCount); printf("学生列表:\n"); for (int i = 0; i < c.studentCount; i++) { printf(" - "); printStudent(c.students[i]); } }

文件操作

文件读写操作
#include <stdio.h> #include <stdlib.h> // 学生结构体 struct Student { int id; char name[50]; int age; float score; }; int main() { FILE *file; struct Student students[3] = { {1001, "Alice", 20, 85.5}, {1002, "Bob", 21, 92.0}, {1003, "Charlie", 19, 78.5} }; // 写入文件 - 二进制模式 file = fopen("students.dat", "wb"); if (file == NULL) { printf("无法创建文件!\n"); return 1; } // 写入学生数据 fwrite(students, sizeof(struct Student), 3, file); fclose(file); printf("学生数据已写入文件\n"); // 读取文件 - 二进制模式 struct Student readStudents[3]; file = fopen("students.dat", "rb"); if (file == NULL) { printf("无法打开文件!\n"); return 1; } fread(readStudents, sizeof(struct Student), 3, file); fclose(file); printf("\n从文件读取的学生数据:\n"); for (int i = 0; i < 3; i++) { printf("学号: %d, 姓名: %s, 年龄: %d, 成绩: %.1f\n", readStudents[i].id, readStudents[i].name, readStudents[i].age, readStudents[i].score); } // 文本文件操作 file = fopen("report.txt", "w"); if (file == NULL) { printf("无法创建文本文件!\n"); return 1; } // 写入文本数据 fprintf(file, "学生成绩报告\n"); fprintf(file, "=============\n"); for (int i = 0; i < 3; i++) { fprintf(file, "%d\t%s\t%d\t%.1f\n", readStudents[i].id, readStudents[i].name, readStudents[i].age, readStudents[i].score); } fclose(file); printf("\n文本报告已生成\n"); // 读取文本文件 printf("\n读取文本文件内容:\n"); file = fopen("report.txt", "r"); if (file == NULL) { printf("无法打开文本文件!\n"); return 1; } char line[100]; while (fgets(line, sizeof(line), file) != NULL) { printf("%s", line); } fclose(file); // 文件追加模式 file = fopen("report.txt", "a"); if (file == NULL) { printf("无法打开文件进行追加!\n"); return 1; } fprintf(file, "\n报告生成时间: 2025年\n"); fclose(file); printf("\n文件追加完成\n"); return 0; }
提示: 这是一个重要的概念,需要特别注意理解和掌握。
注意: 这是一个常见的错误点,请避免犯同样的错误。