在做疫情管理系统时,学生信息文件无法正常读取,可以写入。就是这个系统只能用一次。因为代码较长,检查了一上午也不知道哪里错了,希望有人可以耐心看完代码(由于程序还未做完,只有管理员系统的信息读入,信息显示部分完成,管理员密码123456。有部分代码无用,除了文件外的函数基本没有问题,文件函数感觉也没问题。个人感觉是while循环,以及自己定义turn函数来跳转导致文件无法正确读取)。我是真的想知道哪里错了,希望可以有人能在较短时间帮我解决,并可以指出具体错误。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define SIZE 50
int studentsnum=0;
//学生人数
int dailynum=0;
//每日健康监测条数
int vacnum;//疫苗信息数量
int natnum;//核酸检测信息数量
//声明全局变量
struct student
{
char id[8];
char password[7];
char name[SIZE];
char tel[SIZE];
char dep[SIZE];
char cla[SIZE];
} stu[SIZE];
//学生信息
struct daily
{
char id[8];
char date[SIZE];
float mtemp[SIZE];//早体温
float ntemp[SIZE];//中午体温
float etemp[SIZE];//晚上体温
int cough;//咳嗽
int picture;//健康码
} day[SIZE];
void menu1();
void menu2();
void menu3();
void showStu(struct student *stu);
int passwordStu(struct student *stu);
void stuDocOut(struct student *stu);
void stuHealthOut(struct daily *day);
void stuDocIn(struct student *stu);
void stuHealthIn(struct daily *day);
void writeDaily(struct student *stu, struct daily *day) ;
int passwordAd();
int systemAd(struct student *stu, struct daily *day);
void add(struct student *stu);
void delete1(struct daily *day,struct student *stu);
void stuInformation(struct student *stu, struct daily *day);
void dailyInformation(struct daily *day );
/*struct nat
{
char id;
char date[50];
int result;
} test[50];
//核酸检测结果
struct vaccine
{
char id;
int num;
int type;
} vac[50];
//疫苗接种次数
struct leave
{
int id;
char reason[50][50];
char leavetime[50][50];
char returntime[50][50];
int result;
} leave[50];
//离校申请
*/
void turn1()
{
system("cls");
int choice;
menu1();
scanf("%d",&choice);
switch(choice)
{
case 1:if(passwordAd() == 0)
{
systemAd(stu, day);
}
else
{
printf("密码输入错误!\n");
system("pause");
turn1();
}
break;
//进入管理员端
/*case 2:if(passwordStu(stu) == 0)
{
systemStu(stu, day);
}
else
{
printf("账户或密码输入错误!\n");
}
break;*/
//进入学生端*/
case 3:
stuHealthOut(day);
stuDocOut(stu);
printf("退出成功!\n");
system("pause");
exit(0);
//保存文件数据
}
}//转到主界面
void turn2()
{
int choice;
menu2();
printf("请输入:\n");
scanf("%d",&choice);
switch (choice)
{
case 1:
stuInformation(stu,day);//转至学生信息管理
break;
case 2:
system("cls");
dailyInformation(day);//转至每日健康管理
system("pause");
break;
case 3:
showStu(stu);
system("pause");
break;
case 4:turn1();//转到主界面
break;
}
}//转到管理员界面
int main()
{
void stuDocIn(struct student *stu);
void stuHealthIn(struct daily *day);//从文件中读取数据
while(1)
{
turn1();
}
//密码登录系统
return 0;
}
int passwordAd()
{
char password[7];
{
printf("\n请输入您的六位密码\n");
scanf("%s",password);
if(strcmp(password,"123456") == 0)
{
return 0;
}
//默认管理员密码为123456
else
{
return 1;
}
}
}
//管理员密码验证
int systemAd(struct student *stu, struct daily *day)
{
while(1)
{
turn2();
}
return 0;
}//管理员系统
void add(struct student *stu)
{
studentsnum=studentsnum+1;
printf("请输入学生学号:\n");
scanf("%s",stu[studentsnum-1].id);
printf("请输入学生密码:\n");
scanf("%s",stu[studentsnum-1].password);
printf("%s",stu[studentsnum-1].password);
printf("%d",studentsnum);
printf("请输入学生姓名:\n");
scanf("%s",stu[studentsnum-1].name);
printf("请输入学生电话:\n");
scanf("%s",stu[studentsnum-1].tel);
printf("请输入学生院系:\n");
scanf("%s",stu[studentsnum-1].dep);
printf("请输入学生班级:\n");
scanf("%s",stu[studentsnum-1].cla);
system("cls");
printf("添加成功\n");
}
void delete1(struct daily *day,struct student *stu)
{
int i,j;
printf("请输入删除对象的8位学号:\n");
char id[8];
scanf("%s",id);
for(i = 0;i < dailynum;i++)
{
if(strcmp(id,day[i].id) == 0)
{
for(j = i;j < dailynum-1;j++)
{
day[j] = day[j+1];
}
dailynum--;
i--;
}
}
for(int i = 0;i <studentsnum;i++)
{
if(strcmp(id,stu[i].id) == 0)
{
for(int j = i;j < studentsnum- 1;j++)
{
stu[j] = stu[j+1];
}
studentsnum--;
break;
}
}
}
void stuInformation(struct student *stu, struct daily *day)
{
while(1)
{
menu3();
int choice;
scanf("%d",&choice);
switch (choice)
{
case 1:
add(stu);
break;
case 2:
delete1(day,stu);
break;
case 3:
turn2();
break;
}
}
}
void dailyInformation(struct daily *day )
{
int i;
int count=0;
printf(" 每日健康监测 \n");
printf("****************************************************************************************\n");
printf("学号 日期 早上体温 中午体温 晚上体温 咳嗽 健康码\n");
for(i = 0;i < dailynum;i++)
{
printf("%s %s %f %f %f %d %d",day[i].id,day[i].date[50],
day[i].mtemp,day[i].ntemp,day[i].etemp,day[i].cough,day[i].picture);
count++;
}
if(count==0)
{
printf("当前尚无记录");
}
}
void showStu(struct student *stu)
{
printf("所有学生信息 \n");
printf("*************************************************\n");
printf("学号 姓名 密码 电话 院系 班级\n");
int cnt = 0;
for(int i = 0;i < studentsnum;i++)
{
printf("%s %s %s %s %s %s",stu[i].id,stu[i].name,stu[i].password,stu[i].tel,stu[i].dep,stu[i].cla);
}
}
int passwordStu(struct student *stu)
{
int i,count=0;//计数器,用于判断学号是否正确
char id[10];
char password[7];
printf("\n请输入账户: \n");
scanf("%s",id);
printf("\n请输入六位密码\n");
scanf("%s",password);
for(i = 0;i <studentsnum;i++)
{
if(strcmp(id,stu[i].id) == 0)
{
count=i;
break;
}
}//判断学号是否正确
if(count==studentsnum)
{
return 1;
}//学号不正确则退出
if(strcmp(stu[count].password,password) == 0)
{
return 0;
}
else
{
return 1;
}//判断密码是否正确
}//学生账户密码验证
void stuDocIn(struct student *stu)
{
FILE *fp;
if((fp = fopen("students.txt","r")) == NULL)
{
printf("读取失败!\n");
}
else
{
fscanf(fp,"%d\n",&studentsnum);//读取学生数量
for(int i = 0;i < studentsnum;i++)
{
fscanf(fp,"%s %s %s %s %s %s ",stu[i].id,stu[i].name,stu[i].password,stu[i].tel,stu[i].dep,stu[i].cla);
} //读取每一条学生数据
}
fclose(fp); //关闭文件
}//读取学生数据文件
void stuHealthIn(struct daily *day)
{
FILE *fp;
if((fp = fopen("daily.txt","r")) == NULL)
{
printf("读取失败!\n");
}
else
{
fscanf(fp,"%d",&dailynum);
for(int i = 0;i < dailynum;i++)
{
fscanf(fp,"%s %s %d %d %d %d %d",day[i].id,day[i].date,&day[i].mtemp,&day[i].ntemp,
&day[i].etemp,&day[i].cough,&day[i].picture);
printf("%s %s %s %s %s %s",stu[i].id,stu[i].name,stu[i].password,stu[i].tel,stu[i].dep,stu[i].cla);
}
}
fclose(fp);
}//读取每日健康监测
void stuDocOut(struct student *stu)
{
int i;
FILE *fp;
if((fp = fopen("students.txt","w+")) == NULL)
{
printf("写入失败!\n");
}
else
{
fprintf(fp,"%d\n",studentsnum);
for(i = 0;i < studentsnum;i++)
{
fprintf(fp,"%s %s %s %s %s %s\n",stu[i].id,stu[i].name,stu[i].password,stu[i].tel,stu[i].dep,stu[i].cla);
}
}
fclose(fp);
}
void stuHealthOut(struct daily *day)
{
int i=0;
FILE *fp;
if((fp = fopen("daily.txt","w+")) == NULL)
{
printf("写入失败!\n");
}
else
{
fprintf(fp,"%d\n",dailynum);
for(i = 0;i < dailynum;i++)
{
fprintf(fp,"%s %s %d %d %d %d %d\n",&day[i].id,&day[i].date[50],&day[i].mtemp,&day[i].ntemp,
&day[i].etemp,&day[i].cough,&day[i].picture);
}
}
fclose(fp);
}//保存每日健康检测数据文件
void writeDaily(struct student *stu, struct daily *day)
{
strcpy(day[dailynum].id,stu[studentsnum].id);
printf("日期:\n");
scanf("%s",&day[dailynum].date);
printf("早上温度: \n");
scanf("%f",&day[dailynum].mtemp);
printf("中午温度: \n");
scanf("%f",&day[dailynum].ntemp);
printf("晚上温度: \n");
scanf("%f",&day[dailynum].etemp);
printf("是否咳嗽: 咳嗽填0,不咳嗽填1/n");
scanf("%d",&day[dailynum].cough);
printf("健康码是否为绿码: 是填0,不是填1\n");
scanf("%d",&day[dailynum].picture);
dailynum++;
}//填写每日健康检测数据
void menu1()
{ system("cls");
printf(" 欢迎使用校园疫情防控系统,请选择以下功能\n");
printf("************************************************************************************************************************\n");
printf("1.管理员登录\n");
printf("2.用户登录\n");
printf("3.退出系统\n");
printf("请选择:");
}//主界面
void menu2()
{
system("cls");
printf("欢迎你,管理员,请选择以下功能\n");
printf("***********************************************\n");
printf("1.学生信息管理\n");
printf("2.查看填报记录\n");
printf("3.查看全部学生信息\n");
printf("4.返回主菜单\n");
}
void menu3()
{
system("cls");
printf("请选择您的操作\n");
printf("************************************\n");
printf("1.添加学生\n");
printf("2.删除学生\n");
printf("3.取消\n");
}