c语言学生管理系统代码,可以在原有txt文件中实现对学生的增删查改,对缴费信息的增删查改,可以列表显示所有学生
时间: 2024-04-16 11:28:07 浏览: 92
下面是一个示例的C语言学生管理系统代码,可以在原有的txt文件中实现对学生信息和缴费信息的增删查改,并能够列表显示所有学生:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char studentID[10];
char name[20];
int age;
char paymentStatus[10];
} Student;
void addStudent(FILE *file) {
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);
printf("Enter payment status (Paid/Unpaid): ");
scanf("%s", newStudent.paymentStatus);
fprintf(file, "%s %s %d %s\n", newStudent.studentID, newStudent.name, newStudent.age, newStudent.paymentStatus);
printf("Student added successfully.\n");
}
void deleteStudent(FILE *file) {
char studentID[10];
char line[100];
FILE *tempFile = fopen("temp.txt", "w");
printf("Enter student ID to delete: ");
scanf("%s", studentID);
while (fgets(line, sizeof(line), file)) {
if (strncmp(line, studentID, strlen(studentID)) != 0) {
fputs(line, tempFile);
}
}
fclose(file);
fclose(tempFile);
remove("students.txt");
rename("temp.txt", "students.txt");
printf("Student deleted successfully.\n");
}
void displayStudents(FILE *file) {
char line[100];
printf("---------- Student List ----------\n");
while (fgets(line, sizeof(line), file)) {
printf("%s", line);
}
printf("-----------------------------------\n");
}
int main() {
FILE *file = fopen("students.txt", "a+");
int choice;
if (file == NULL) {
printf("Error opening file.\n");
return 1;
}
while (1) {
printf("\n----- Student Management System -----\n");
printf("1. Add student\n");
printf("2. Delete student\n");
printf("3. Display all students\n");
printf("0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addStudent(file);
break;
case 2:
deleteStudent(file);
break;
case 3:
displayStudents(file);
break;
case 0:
fclose(file);
return 0;
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
```
请注意,该示例代码使用了简单的文本文件(students.txt)来存储学生信息。每个学生的信息在文件中占据一行,格式为:学生ID 学生姓名 学生年龄 缴费状态。您可以根据需要进行修改和扩展,以适应实际的学生管理需求。
阅读全文