c语言学生管理系统代码要求在原有的学生表中增删学生,学生索引文件也会跟着变化,修改学生信息,按学号查找学生,实现清屏功能
时间: 2024-04-16 21:28:34 浏览: 102
以下是一个示例的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`)。
请根据您的实际需求进行修改和扩展。
阅读全文