c语言学生管理系统代码要求在原有的学生表中增删学生,学生索引文件也会跟着变化,修改学生信息,按学号查找学生,实现清屏功能

时间: 2024-04-16 15:28:34 浏览: 21
以下是一个示例的C语言学生管理系统代码,满足您的要求,并添加了清屏功能: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { char studentID[10]; char name[20]; int age; } Student; void clearScreen() { system("clear"); // for Linux/Mac //system("cls"); // for Windows } void addStudent(FILE *dataFile, FILE *indexFile) { Student newStudent; printf("Enter student ID: "); scanf("%s", newStudent.studentID); printf("Enter student name: "); scanf("%s", newStudent.name); printf("Enter student age: "); scanf("%d", &newStudent.age); fseek(dataFile, 0, SEEK_END); fwrite(&newStudent, sizeof(Student), 1, dataFile); fprintf(indexFile, "%s\n", newStudent.studentID); printf("Student added successfully.\n"); } void deleteStudent(FILE *dataFile, FILE *indexFile) { char studentID[10]; char line[100]; FILE *tempIndexFile = fopen("temp_index.txt", "w"); FILE *tempDataFile = fopen("temp_data.bin", "wb"); printf("Enter student ID to delete: "); scanf("%s", studentID); rewind(indexFile); while (fgets(line, sizeof(line), indexFile)) { if (strncmp(line, studentID, strlen(studentID)) != 0) { fputs(line, tempIndexFile); } else { fseek(dataFile, ftell(dataFile), SEEK_SET); fwrite("\0", sizeof(Student), 1, dataFile); // mark as deleted } } fclose(indexFile); fclose(dataFile); fclose(tempIndexFile); fclose(tempDataFile); remove("students_index.txt"); remove("students_data.bin"); rename("temp_index.txt", "students_index.txt"); rename("temp_data.bin", "students_data.bin"); indexFile = fopen("students_index.txt", "r"); dataFile = fopen("students_data.bin", "ab+"); printf("Student deleted successfully.\n"); } void modifyStudent(FILE *dataFile, FILE *indexFile) { char studentID[10]; Student newStudent; printf("Enter student ID to modify: "); scanf("%s", studentID); while (fread(&newStudent, sizeof(Student), 1, dataFile)) { if (strncmp(newStudent.studentID, studentID, strlen(studentID)) == 0) { printf("Enter new student name: "); scanf("%s", newStudent.name); printf("Enter new student age: "); scanf("%d", &newStudent.age); fseek(dataFile, -sizeof(Student), SEEK_CUR); fwrite(&newStudent, sizeof(Student), 1, dataFile); printf("Student modified successfully.\n"); return; } } printf("Student not found.\n"); } void searchStudent(FILE *dataFile, FILE *indexFile) { char studentID[10]; Student foundStudent; printf("Enter student ID to search: "); scanf("%s", studentID); while (fread(&foundStudent, sizeof(Student), 1, dataFile)) { if (strncmp(foundStudent.studentID, studentID, strlen(studentID)) == 0) { printf("Student ID: %s\n", foundStudent.studentID); printf("Name: %s\n", foundStudent.name); printf("Age: %d\n", foundStudent.age); return; } } printf("Student not found.\n"); } void displayStudents(FILE *dataFile, FILE *indexFile) { char studentID[10]; Student student; rewind(indexFile); while (fgets(studentID, sizeof(studentID), indexFile)) { fseek(dataFile, atoi(studentID) * sizeof(Student), SEEK_SET); fread(&student, sizeof(Student), 1, dataFile); if (strlen(student.studentID) > 0) { printf("Student ID: %s\n", student.studentID); printf("Name: %s\n", student.name); printf("Age: %d\n", student.age); printf("-----------------------\n"); } } } int main() { FILE *dataFile = fopen("students_data.bin", "ab+"); FILE *indexFile = fopen("students_index.txt", "a+"); int choice; if (dataFile == NULL || indexFile == NULL) { printf("Error opening file.\n"); return 1; } while (1) { clearScreen(); printf("----- Student Management System -----\n"); printf("1. Add student\n"); printf("2. Delete student\n"); printf("3. Modify student\n"); printf("4. Search student\n"); printf("5. Display all students\n"); printf("0. Exit\n"); printf("Enter your choice: "); scanf("%d", &choice); switch (choice) { case 1: addStudent(dataFile, indexFile); break; case 2: deleteStudent(dataFile, indexFile); break; case 3: modifyStudent(dataFile, indexFile); break; case 4: searchStudent(dataFile, indexFile); break; case 5: displayStudents(dataFile, indexFile); break; case 0: fclose(dataFile); fclose(indexFile); return 0; default: printf("Invalid choice. Please try again.\n"); } getchar(); // clear newline character printf("Press enter to continue..."); getchar(); } return 0; } ``` 请注意,该示例代码使用了两个文件(students_data.bin 和 students_index.txt)来存储学生信息和学生索引。students_data.bin 文件存储了学生结构体的二进制数据,students_index.txt 文件存储了学生的索引信息(学生ID)。每个学生的信息在文件中占据一块固定大小的空间,可在文件中随机访问。删除学生时,将其对应的数据块标记为删除状态,而不是实际删除。这种方式可以在一定程度上减少文件的碎片化,并提高文件的读取效率。 此外,本示例还添加了清屏功能,使用 `clearScreen()` 函数在终端中清除屏幕内容。请根据您的操作系统选择适用的清屏命令(Linux/Mac 上为 `clear`,Windows 上为 `cls`)。 请根据您的实际需求进行修改和扩展。

相关推荐

最新推荐

recommend-type

C语言数组实现学生信息管理系统设计

主要为大家详细介绍了C语言数组实现学生信息管理系统设计,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

C语言实现简单学生学籍管理系统

主要为大家详细介绍了C语言实现简单学生学籍管理系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

C语言实现简单学生选课管理系统

主要为大家详细介绍了C语言实现简单学生选课管理系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

学生成绩管理系统(数据结构)实验报告.docx

期末实践周必备(数据结构)学生成绩管理系统,基于C语言完成的,百分百作者原创,资源保证!
recommend-type

C语言实现学生选课系统完整版

主要为大家详细介绍了C语言实现学生选课系统的完整版,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

hive中 的Metastore

Hive中的Metastore是一个关键的组件,它用于存储和管理Hive中的元数据。这些元数据包括表名、列名、表的数据类型、分区信息、表的存储位置等信息。Hive的查询和分析都需要Metastore来管理和访问这些元数据。 Metastore可以使用不同的后端存储来存储元数据,例如MySQL、PostgreSQL、Oracle等关系型数据库,或者Hadoop分布式文件系统中的HDFS。Metastore还提供了API,使得开发人员可以通过编程方式访问元数据。 Metastore的另一个重要功能是跟踪表的版本和历史。当用户对表进行更改时,Metastore会记录这些更改,并且可以让用户回滚到
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。