上一篇文章介绍了C语言写的学生信息管理系统,但有一个很严重的问题就是数据只保存到内存中,没有保存到硬盘中,关闭程序再次打开时候,所有的数据都会清空。
学生信息管理系统——C语言版
针对上诉问题,现对代码进行了修改,其他需求和文件目录可以参考上一篇文章。
文件内容
1 明轩 19 男 101
2 雅琳 18 女 105
3 佳晨 17 男 103
4 晨曦 16 女 102
5 惜晴 15 女 101
6 春儿 18 女 104
7 梦琪 17 女 103
8 英豪 16 男 102
9 春冬 15 女 105
10 桃之 19 女 104
运行程序时,将该文件放置到可执行程序的同级目录
// student.h
#ifndef STUDENT_H
#define STUDENT_H
typedef struct {
int id;
char name[50];
int age;
char gender[10];
char class_name[50];
} Student;
void save_students_to_file(Student students[], int num_students);
void load_students_from_file(Student students[], int *num_students);
void add_student(Student students[], int *num_students);
void query_student(Student students[], int num_students);
void modify_student(Student students[], int num_students);
void delete_student(Student students[], int *num_students);
void display_students(Student students[], int num_students);
#endif // STUDENT_H
// student.c
#include
#include
#include "student.h"
void save_students_to_file(Student students[], int num_students) {
FILE *file = fopen("students.txt", "w");
if (file == NULL) {
printf("无法打开文件用于写入\n");
return;
}
for (int i = 0; i < num_students; i++) {
fprintf(file, "%d %s %d %s %s\n", students[i].id, students[i].name, students[i].age, students[i].gender, students[i].class_name);
}
fclose(file);
printf("学生信息已保存到文件。\n");
}
void load_students_from_file(Student students[], int *num_students) {
FILE *file = fopen("students.txt", "r");
if (file == NULL) {
printf("无法打开文件用于读取\n");
return;
}
*num_students = 0;
while (fscanf(file, "%d %s %d %s %s\n", &students[*num_students].id, students[*num_students].name, &students[*num_students].age, students[*num_students].gender, students[*num_students].class_name) == 5) {
(*num_students)++;
}
fclose(file);
printf("从文件加载学生信息。\n");
}
void add_student(Student students[], int *num_students) {
printf("请输入学生的学号: ");
scanf("%d", &students[*num_students].id);
printf("请输入学生的姓名: ");
scanf("%s", students[*num_students].name);
printf("请输入学生的年龄: ");
scanf("%d", &students[*num_students].age);
printf("请输入学生的性别: ");
scanf("%s", students[*num_students].gender);
printf("请输入学生的班级: ");
scanf("%s", students[*num_students].class_name);
(*num_students)++;
}
void query_student(Student students[], int num_students) {
int choice;
printf("请选择查询方式:\n");
printf("1. 通过学号查询\n");
printf("2. 通过姓名查询\n");
printf("请输入你的选择: ");
scanf("%d", &choice);
if (choice == 1) {
int id;
printf("请输入学生的学号: ");
scanf("%d", &id);
for (int i = 0; i < num_students; i++) {
if (students[i].id == id) {
printf("学号: %d, 姓名: %s, 年龄: %d, 性别: %s, 班级: %s\n",
students[i].id, students[i].name, students[i].age, students[i].gender, students[i].class_name);
return;
}
}
printf("未找到该学生信息\n");
} else if (choice == 2) {
char name[50];
printf("请输入学生的姓名: ");
scanf("%s", name);
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("学号: %d, 姓名: %s, 年龄: %d, 性别: %s, 班级: %s\n",
students[i].id, students[i].name, students[i].age, students[i].gender, students[i].class_name);
return;
}
}
printf("未找到该学生信息\n");
} else {
printf("无效的选择\n");
}
}
void modify_student(Student students[], int num_students) {
int id;
printf("请输入要修改的学生的学号: ");
scanf("%d", &id);
int index = -1;
for (int i = 0; i < num_students; i++) {
if (students[i].id == id) {
index = i;
break;
}
}
if (index != -1) {
printf("请输入学生的新姓名: ");
scanf("%s", students[index].name);
printf("请输入学生的新年龄: ");
scanf("%d", &students[index].age);
printf("请输入学生的新性别: ");
scanf("%s", students[index].gender);
printf("请输入学生的新班级: ");
scanf("%s", students[index].class_name);
printf("学生信息已更新。\n");
} else {
printf("未找到该学生信息。\n");
}
}
void delete_student(Student students[], int *num_students) {
int id;
printf("请输入要删除的学生的学号: ");
scanf("%d", &id);
int index = -1;
for (int i = 0; i < *num_students; i++) {
if (students[i].id == id) {
index = i;
break;
}
}
if (index != -1) {
for (int i = index; i < (*num_students) - 1; i++) {
students[i] = students[i + 1];
}
(*num_students)--;
printf("学生信息已删除。\n");
} else {
printf("未找到该学生信息。\n");
}
}
void display_students(Student students[], int num_students) {
printf("+--------+--------+------+-------+------------+\n");
printf("| 学号 | 姓名 | 年龄 | 性别 | 班级 |\n");
printf("+--------+--------+------+-------+------------+\n");
for (int i = 0; i < num_students; i++) {
printf("| %-6d | %-6s | %-4d | %-5s | %-10s |\n",
students[i].id, students[i].name, students[i].age, students[i].gender, students[i].class_name);
}
printf("+--------+--------+------+-------+------------+\n");
}
// main.c
#include
#include "student.h"
int main() {
Student students[100];
int num_students = 0;
load_students_from_file(students, &num_students);
while (1) {
printf("欢迎使用学生信息管理系统\n");
printf("请选择一个操作:\n");
printf("1. 添加学生信息\n");
printf("2. 查询学生信息\n");
printf("3. 修改学生信息\n");
printf("4. 删除学生信息\n");
printf("5. 显示所有学生信息\n");
printf("6. 退出系统\n");
printf("请输入你的选择: ");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
add_student(students, &num_students);
save_students_to_file(students, num_students);
break;
case 2:
query_student(students, num_students);
break;
case 3:
modify_student(students, num_students);
save_students_to_file(students, num_students);
break;
case 4:
delete_student(students, &num_students);
save_students_to_file(students, num_students);
break;
case 5:
display_students(students, num_students);
break;
case 6:
printf("感谢使用学生信息管理系统!再见!\n");
return 0;
default:
printf("无效的选择,请重新输入。\n");
}
}
return 0;
}