源码如下:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#define LEN sizeof(Student)
#define Room_num 500
typedef struct Student //确认结构体
{
long long int Id; //学号
char Name[10]; //姓名
char Sex[10]; //性别
char BirthPlace[10]; //籍贯
char ZhuanYe[10]; //专业
}Student;
void Title();//标题函数
void Menu();//菜单函数
void WriteToFile();//信息输入的函数
void ReadFromFile();//显示所有信息的函数
void QueryFile();//查询函数
void ModifyFile();//修改函数
void DeletFile();//删除数据的函数
int main()
{
int select;
do
{
system("cls");
Title();
Menu();
scanf("%d", &select);
switch (select)
{
case 1:
WriteToFile();
break;
case 2:
ReadFromFile();
break;
case 3:
QueryFile();
break;
case 4:
ModifyFile();
break;
case 5:
DeletFile();
break;
default:
printf("退出程序!");
exit(0);
break;
}
} while ((select == 1 || select == 2) || (select == 3 || select == 4) || (select == 5));
return 0;
}//利用switch函数进行菜单的选择
void Title()
{
//int num = Left();
printf("**********欢迎使用学籍管理系统***********\n");
}//标题函数1
void Menu()
{
printf("********************系统功能菜单********************\n");
printf("* *\n");
printf("* 1.录入学生信息 *\n");
printf("* 2.显示所有信息 *\n");
printf("* 3.查询学生信息 *\n");
printf("* 4.修改学生信息 *\n");
printf("* 5.删除学生信息 *\n");
printf("* 0.退出系统 *\n");
printf("* *\n");
printf("———————————————————————--------------------—------——\n\n");
printf("请选择菜单编号:");
}//标题函数2
void WriteToFile()
{
FILE* fp = NULL;
Student stu = { 0 };
char flag = 'y';
fp = fopen("book1.dat", "ab+");//打开文件
if (fp == NULL)
{
printf("文件打开失败!\n");
exit(1);//1表示在有错的方式退出程序
}
while ((flag == 'y' || flag == 'Y'))
{
system("cls");
Title();
printf("请输入证件号id:");
scanf("%lld", &stu.Id);
printf("请输入姓名:");
scanf("%s", stu.Name);
printf("请输入性别:");
scanf("%s", stu.Sex);
printf("请输入籍贯:");
scanf("%s", stu.BirthPlace);
printf("请输入专业:");
scanf("%s", stu.ZhuanYe);
fwrite(&stu, LEN, 1, fp);
fflush(stdin);
printf("继续输入吗?继续请输入y或Y,输入其他则退出:");
getchar();
scanf("%c", &flag);
}
fclose(fp);//关闭文件
return;
}//添加函数
void ReadFromFile()
{
system("cls");
Title();
FILE* fp = NULL;
Student stu;
fp = fopen("book1.dat", "rb");
if (fp == NULL)
{
printf("文件打开失败");
exit(1);
}
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("\tid\t姓名\t性别\t籍贯\t专业\n");
fseek(fp, 0, SEEK_SET);
while (!feof(fp))
{
if (fread(&stu, LEN, 1, fp))
{
printf("%lld\t%s\t%s\t%s\t%s\n", stu.Id, stu.Name, stu.Sex, stu.BirthPlace, stu.ZhuanYe);
}
}
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
system("pause");
system("cls");
fclose(fp);
return;
}
void QueryFile()
{
system("cls");
Title();
Student stu;
char x[8];
int flag = 0;
FILE* fp;
long long int num;
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("\t\t请输入学生姓名:");
scanf("%s", x);
printf("\tid\t姓名\t性别\t籍贯\t专业\n");
fp = fopen("book1.dat", "rb");
if (fp == NULL)
{
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("错误\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
return;
}
fseek(fp, 0, SEEK_SET);
while (fread(&stu, LEN, 1, fp))
{
if (strcmp(x, stu.Name) == 0)
{
printf("%lld\t%s\t%s\t%s\t%s\n", stu.Id, stu.Name, stu.Sex, stu.BirthPlace, stu.ZhuanYe);
flag = 1;
}
if (flag = 0)
{
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("查无此人");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
}
}
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
fclose(fp);
system("pause");
system("cls");
return;
}
void ModifyFile()
{
system("cls");
Title();
Student stu;
FILE* fp;
char x[8];
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("请输入学生姓名:");
scanf("%s", x);
fp = fopen("book1.dat", "rb+");
if (fp == NULL)
{
printf("文件打开失败");
exit(1);
}
fseek(fp, 0, SEEK_SET);
while (fread(&stu, LEN, 1, fp))
{
if (strcmp(x, stu.Name) == 0)
{
printf("请输入证件号id:");
scanf("%lld", &stu.Id);
printf("请输入姓名:");
scanf("%s", stu.Name);
printf("请输入性别:");
scanf("%s", stu.Sex);
printf("请输入籍贯:");
scanf("%s", stu.BirthPlace);
printf("请输入专业:");
scanf("%s", stu.ZhuanYe);
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
fflush(stdin);
fseek(fp, 0 - LEN, SEEK_CUR);
fwrite(&stu, LEN, 1, fp);
fclose(fp);
}
if (feof(fp))
{
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("没有该学生信息");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
}
}
system("pause");
system("cls");
return;
}
void DeletFile()
{
system("cls");
Title();
Student s;
FILE* fp;
fp = fopen("book1.dat", "rb+");
if (fp == NULL)
{
printf("打开文件错误!!!\n");
exit(1);
}
printf("\n请输入学生ID:");
long long int num;
scanf("%lld", &num);
printf("\n\t\t\t删除成功\n");
fseek(fp, 0, SEEK_SET);
FILE* fp1;
fp1 = fopen("./linshi.dat", "ab+");//读写新建一个临时文件
while (fread(&s, LEN, 1, fp))//从原文件读一个结点
{
if (num != s.Id)//不是要删除的内容
{
fwrite(&s, LEN, 1, fp1);
}
}
fclose(fp);
fclose(fp1);
remove("book1.dat");//删除原文件
rename("linshi.dat", "book1.dat");//重命名为原文件
fflush(stdin);
system("pause");
system("cls");
return;
}