使用文件处理存储学生学籍信息,给出代码学生学籍管理系统详细C语言包含学号姓名身份证号电话号码籍贯通讯地址录入学生信息,删除,修改查询等功能的学生学籍管理系统
时间: 2024-03-14 07:47:55 浏览: 119
好的,以下是一个完整的学生学籍管理系统的代码,包括学号、姓名、身份证号、电话号码、籍贯、通讯地址等信息的录入、删除、修改、查询等功能:
``` c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_STUDENT 1000
#define MAX_NAME 20
#define MAX_ID 18
#define MAX_PHONE 15
#define MAX_ADDRESS 50
#define MAX_HOMETOWN 20
typedef struct {
char name[MAX_NAME]; //姓名
char id[MAX_ID]; //身份证号
char phone[MAX_PHONE]; //电话号码
char address[MAX_ADDRESS]; //通讯地址
char hometown[MAX_HOMETOWN]; //籍贯
} Student;
void print_menu();
void add_student(Student* students, int* n);
void delete_student(Student* students, int* n);
void modify_student(Student* students, int* n);
void query_student(Student* students, int n);
int main() {
Student students[MAX_STUDENT];
int n = 0; //学生数目
char filename[MAX_NAME];
printf("请输入要存储的文件名:");
scanf("%s", filename);
FILE* fp = fopen(filename, "w");
if (fp == NULL) {
printf("文件打开失败!\n");
exit(1);
}
fclose(fp);
printf("欢迎使用学生学籍管理系统!\n");
while (1) {
print_menu();
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
add_student(students, &n);
break;
case 2:
delete_student(students, &n);
break;
case 3:
modify_student(students, &n);
break;
case 4:
query_student(students, n);
break;
case 5:
printf("感谢使用学生学籍管理系统!\n");
exit(0);
default:
printf("无效选择,请重新输入!\n");
break;
}
}
return 0;
}
void print_menu() {
printf("\n");
printf("学生学籍管理系统\n");
printf("1. 录入学生信息\n");
printf("2. 删除学生信息\n");
printf("3. 修改学生信息\n");
printf("4. 查询学生信息\n");
printf("5. 退出系统\n");
printf("请选择:");
}
void add_student(Student* students, int* n) {
if (*n >= MAX_STUDENT) {
printf("学生数目已达到上限,无法继续录入!\n");
return;
}
printf("请输入学生姓名(输入0结束录入):");
scanf("%s", students[*n].name);
if (strcmp(students[*n].name, "0") == 0) {
return;
}
printf("请输入学生身份证号:");
scanf("%s", students[*n].id);
printf("请输入学生电话号码:");
scanf("%s", students[*n].phone);
printf("请输入学生通讯地址:");
scanf("%s", students[*n].address);
printf("请输入学生籍贯:");
scanf("%s", students[*n].hometown);
FILE* fp = fopen("students.txt", "a");
if (fp == NULL) {
printf("文件打开失败!\n");
exit(1);
}
fprintf(fp, "%s %s %s %s %s\n", students[*n].name, students[*n].id, students[*n].phone, students[*n].address, students[*n].hometown);
fclose(fp);
*n = *n + 1;
printf("学生信息已录入!\n");
}
void delete_student(Student* students, int* n) {
if (*n == 0) {
printf("学生数目为0,无法删除!\n");
return;
}
char name[MAX_NAME];
printf("请输入要删除的学生姓名:");
scanf("%s", name);
int i;
for (i = 0; i < *n; i++) {
if (strcmp(students[i].name, name) == 0) {
break;
}
}
if (i == *n) {
printf("未找到该学生!\n");
return;
}
FILE* fp1 = fopen("students.txt", "r");
if (fp1 == NULL) {
printf("文件打开失败!\n");
exit(1);
}
FILE* fp2 = fopen("temp.txt", "w");
if (fp2 == NULL) {
printf("文件打开失败!\n");
exit(1);
}
char line[MAX_NAME + MAX_ID + MAX_PHONE + MAX_ADDRESS + MAX_HOMETOWN + 10];
while (fgets(line, sizeof(line), fp1) != NULL) {
if (strstr(line, name) == NULL) {
fputs(line, fp2);
}
}
fclose(fp1);
fclose(fp2);
remove("students.txt");
rename("temp.txt", "students.txt");
*n = *n - 1;
printf("学生信息已删除!\n");
}
void modify_student(Student* students, int* n) {
if (*n == 0) {
printf("学生数目为0,无法修改!\n");
return;
}
char name[MAX_NAME];
printf("请输入要修改的学生姓名:");
scanf("%s", name);
int i;
for (i = 0; i < *n; i++) {
if (strcmp(students[i].name, name) == 0) {
break;
}
}
if (i == *n) {
printf("未找到该学生!\n");
return;
}
printf("请输入新的学生姓名:");
scanf("%s", students[i].name);
printf("请输入新的学生身份证号:");
scanf("%s", students[i].id);
printf("请输入新的学生电话号码:");
scanf("%s", students[i].phone);
printf("请输入新的学生通讯地址:");
scanf("%s", students[i].address);
printf("请输入新的学生籍贯:");
scanf("%s", students[i].hometown);
FILE* fp1 = fopen("students.txt", "r");
if (fp1 == NULL) {
printf("文件打开失败!\n");
exit(1);
}
FILE* fp2 = fopen("temp.txt", "w");
if (fp2 == NULL) {
printf("文件打开失败!\n");
exit(1);
}
char line[MAX_NAME + MAX_ID + MAX_PHONE + MAX_ADDRESS + MAX_HOMETOWN + 10];
while (fgets(line, sizeof(line), fp1) != NULL) {
if (strstr(line, name) == NULL) {
fputs(line, fp2);
} else {
fprintf(fp2, "%s %s %s %s %s\n", students[i].name, students[i].id, students[i].phone, students[i].address, students[i].hometown);
}
}
fclose(fp1);
fclose(fp2);
remove("students.txt");
rename("temp.txt", "students.txt");
printf("学生信息已修改!\n");
}
void query_student(Student* students, int n) {
if (n == 0) {
printf("学生数目为0,无法查询!\n");
return;
}
char name[MAX_NAME];
printf("请输入要查询的学生姓名:");
scanf("%s", name);
int i;
for (i = 0; i < n; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("姓名:%s\n", students[i].name);
printf("身份证号:%s\n", students[i].id);
printf("电话号码:%s\n", students[i].phone);
printf("通讯地址:%s\n", students[i].address);
printf("籍贯:%s\n", students[i].hometown);
return;
}
}
printf("未找到该学生!\n");
}
```
这个程序使用了一个菜单来提供录入、删除、修改和查询学生信息的功能。用户可以选择不同的选项来执行不同的操作。程序中使用了结构体来存储学生信息,同时使用了文件处理函数将学生信息写入到文件中,并且使用了文件读取函数来查询、删除和修改学生信息。
阅读全文