c语言学生管理系统代码用到文件并能生成索引文件,学生表包括学号姓名,初始化的时候在代码中包含一部分学生信息如{20200001,张三},{20200002,李四}等,可以增删查改学生,可以列表所有学生信息,计数的时候记录的是写入文件的代码中包含的学生信息的加上新增的学生人生,可以删除修改代码中包含的学生信息
时间: 2024-04-17 20:25:49 浏览: 127
学生管理系统代码,主要实现学生信息的增删改查等
下面是一个使用文件存储学生信息并生成索引文件的示例代码,其中包括学生表的初始化、增加、删除、修改和查询学生等操作:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
struct Student {
char studentID[10];
char name[50];
};
struct Student students[MAX_STUDENTS];
int studentCount = 0;
void loadStudents() {
FILE *file = fopen("students.txt", "r");
if (file == NULL) {
printf("Failed to open students.txt file!\n");
return;
}
while (fscanf(file, "%s %s", students[studentCount].studentID, students[studentCount].name) == 2) {
studentCount++;
}
fclose(file);
}
void saveStudents() {
FILE *file = fopen("students.txt", "w");
if (file == NULL) {
printf("Failed to open students.txt file!\n");
return;
}
for (int i = 0; i < studentCount; i++) {
fprintf(file, "%s %s\n", students[i].studentID, students[i].name);
}
fclose(file);
}
void generateIndexFile() {
FILE *file = fopen("index.txt", "w");
if (file == NULL) {
printf("Failed to open index.txt file!\n");
return;
}
for (int i = 0; i < studentCount; i++) {
fprintf(file, "%s\n", students[i].studentID);
}
fclose(file);
}
void addStudent() {
if (studentCount == MAX_STUDENTS) {
printf("Maximum number of students reached!\n");
return;
}
struct Student newStudent;
printf("Enter student ID: ");
scanf("%s", newStudent.studentID);
printf("Enter student name: ");
scanf("%s", newStudent.name);
strcpy(students[studentCount].studentID, newStudent.studentID);
strcpy(students[studentCount].name, newStudent.name);
studentCount++;
saveStudents();
generateIndexFile();
printf("Student added successfully!\n");
}
void deleteStudent() {
char studentID[10];
printf("Enter student ID: ");
scanf("%s", studentID);
int found = 0;
for (int i = 0; i < studentCount; i++) {
if (strcmp(students[i].studentID, studentID) == 0) {
for (int j = i; j < studentCount - 1; j++) {
students[j] = students[j + 1];
}
studentCount--;
found = 1;
break;
}
}
if (found) {
saveStudents();
generateIndexFile();
printf("Student deleted successfully!\n");
} else {
printf("Student not found!\n");
}
}
void updateStudent() {
char studentID[10];
printf("Enter student ID: ");
scanf("%s", studentID);
int found = 0;
for (int i = 0; i < studentCount; i++) {
if (strcmp(students[i].studentID, studentID) == 0) {
printf("Enter new name: ");
scanf("%s", students[i].name);
found = 1;
break;
}
}
if (found) {
saveStudents();
printf("Student updated successfully!\n");
} else {
printf("Student not found!\n");
}
}
void searchStudent() {
char studentID[10];
printf("Enter student ID: ");
scanf("%s", studentID);
int found = 0;
for (int i = 0; i < studentCount; i++) {
if (strcmp(students[i].studentID, studentID) == 0) {
printf("Student found:\n");
printf("Student ID: %s\n", students[i].studentID);
printf("Name: %s\n", students[i].name);
found = 1;
break;
}
}
if (!found) {
printf("Student not found!\n");
}
}
void listStudents() {
printf("Student List:\n");
for (int i = 0; i < studentCount; i++) {
printf("Student ID: %s\n", students[i].studentID);
printf("Name: %s\n", students[i].name);
printf("--------------------\n");
}
printf("Total number of students: %d\n", studentCount);
}
int main() {
loadStudents();
int choice;
do {
printf("\nStudent Management System\n");
printf("1. Add Student\n");
printf("2. Delete Student\n");
printf("3. Update Student\n");
printf("4. Search Student\n");
printf("5. List Students\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addStudent();
break;
case 2:
deleteStudent();
break;
case 3:
updateStudent();
break;
case 4:
searchStudent();
break;
case 5:
listStudents();
break;
case 6:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 6);
return 0;
}
```
在这个示例代码中,学生信息存储在名为 `students.txt` 的文件中,索引信息存储在名为 `index.txt` 的索引文件中。在程序初始化时,通过调用 `loadStudents` 函数从文件中读取学生信息,并根据这些信息生成索引文件。
`addStudent` 函数用于添加新学生信息,首先从终端获取学号和姓名,并将其添加到 `students` 数组中,然后调用 `saveStudents` 和 `generateIndexFile` 函数将新信息保存到文件中。
`deleteStudent` 函数用于删除学生信息,从终端获取要删除的学生的学号,然后在 `students` 数组中查找该学生并删除,最后调用 `saveStudents` 和 `generateIndexFile` 函数更新文件和索引文件。
`updateStudent` 函数用于修改学生姓名,从终端获取要修改的学生的学号,在 `students` 数组中查找该学生并修改姓名,然后调用 `saveStudents` 函数保存到文件中。
`searchStudent` 函数用于根据学号查询学生信息,从终端获取要查询的学生的学号,在 `students` 数组中查找该学生并显示信息。
`listStudents` 函数用于列出所有学生的信息,遍历 `students` 数组并显示每个学生的学号和姓名,最后输出学生总人数。
请确保在程序运行前,已经在 `students.txt` 文件中初始化了一部分学生信息。这样,在程序运行时,即可读取并显示已经存在的学生信息,并能够通过终端输入进行增删查改操作。记得根据自己的需求进一步修改和完善代码。
阅读全文