估计你写完已经太累了,根本就没调试。
1)字符数组和字符变量不分,输入输出格式符全混了。
2)用文本方式打开文件,又按二进制格式读写文件。
大致弄了一下。不敢保证没有错的地方。
程序代码:
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#define N 1000
#define M 5
void xiugai();
/*定义学生结构体*/
struct stu
{
int num; //学号
char name[20]; //姓名
char sex[20]; //性别
float mark[M]; //分数
};
struct stu students[N];
int i=0;
/*添加学生信息*/
void students_message()
{
char pan;
int j;
while(1)
{
printf("请输入学号:");
scanf("%d",&students[i].num);
printf("请输入姓名:");
scanf("%s",students[i].name); //-------------
printf("请输入性别:");
scanf("%s",students[i].sex); //-------------
for(j=0;j<M;j++)
{
printf("请输入分数%d:",j+1);
scanf("%f",&students[i].mark[j]);
}
i++;
while(1)
{
printf("是否继续输入?(Y/N)");
scanf("%c",&pan); //-----------------char pan;
getchar();
switch(pan)
{
case 'N':
case 'n':
return;
case 'Y':
case 'y':
break;
default:
printf("错误,请重新输入!\n");
continue;
}
break;
}
}
}
/*修改学生信息*/
void students_recover()
{
int j,k,t;
char xiu[50];
int xiu1;
char choise;
printf("请选择修改类型:姓名查询(a)/学号查询(b)");
scanf("%c",&choise); //-----------------char choise;
switch(choise)
{
case 'a':
printf("请输入姓名:");
scanf("%s",xiu); //-----------------
for(j=0,k=0;j<i;j++) //-----------------注意数据个数
if(strcmp((const char *)xiu,students[j].name)==0) //查询是否存在该姓名
{
printf("请输入新的学号:");
scanf("%d",&students[j].num);
printf("请输入新的姓名:");
scanf("%s",students[j].name); //-----------------
printf("请输入新的性别:");
scanf("%s",students[j].sex); //-----------------
for(t=0;t<M;t++)
{
printf("请输入分数%d:",t+1);
scanf("%f",&students[j].mark[t]);
}
k=1;
break; //-----------------学号和名字应该理解成是唯一的,相互不同的
}
if(k)
break;
else
printf("该学生不存在!\n");
system("pause");
break;
case 'b':
printf("请输入学号:");
scanf("%d",&xiu1);
for(j=0,k=0;j<N;j++)
if(xiu1==students[j].num) //查询是否存在该学号
{
printf("请输入新的学号:");
scanf("%d",&students[j].num);
printf("请输入新的姓名:");
scanf("%s",&students[j].name);
printf("请输入新的性别:");
scanf("%s",&students[j].sex);
for(t=0;t<M;t++)
{
printf("请输入分数%d:",t+1);
scanf("%f",&(students[j].mark[t]));
}
k=1;
break;
}
if(k)
break;
else
printf("该学生不存在!\n");
system("pause");
break;
default:
printf("输入错误!\n");
break;
}
}
/*查询学生信息*/
void students_chaxun1()
{
char choise;
int j,k,t;
int cha1;
char cha[50];
printf("请选择查询类型:姓名查询(a)/学号查询(b)");
scanf("%c",&choise); //--------------char choise;
switch(choise)
{
case 'a':
printf("请输入姓名:"); //输入姓名查询
scanf("%s",cha); //---------
for(j=0,k=0;j<i;j++)//---------
if(strcmp((const char *)cha,students[j].name)==0)
{
printf("\n\n");
printf("--------------------------------------------------------------------\n");
printf("%10s%10s%8s%8s%8s%8s%8s%8s\n","学号","姓名","性别","成绩1","成绩2","成绩3","成绩4","成绩5");
printf("--------------------------------------------------------------------\n");
printf("%10d%10s%8s",students[j].num,students[j].name,students[j].sex);
for(t=0;t<M;t++)
printf("%8.2f",students[j].mark[t]);
printf("\n");
printf("--------------------------------------------------------------------\n");
printf("\n\n");
system("pause");
k=1;
break;
}
if(k)
break;
else
printf("不存在\n");
system("pause");
break;
case 'b':
printf("请输入学号:"); //输入学号查询
scanf("%d",&cha1);
for(j=0,k=0;j<i;j++) //--------------
if(cha1==students[j].num)
{
printf("\n\n");
printf("--------------------------------------------------------------------\n");
printf("%10s%10s%8s%8s%8s%8s%8s\n","学号","姓名","性别","成绩1","成绩2","成绩3","成绩4","成绩5");
printf("--------------------------------------------------------------------\n");
printf("%10d%10s%8s",students[j].num,students[j].name,students[j].sex);
for(t=0;t<M;t++)
printf("%8.2f",students[j].mark[t]);
printf("\n");
printf("--------------------------------------------------------------------\n");
printf("\n\n");
system("pause");
k=1;
break;
}
if(k)
break;
else
printf("不存在\n");
system("pause");
default:
printf("输入错误!\n");
break;
}
}
/*输出学生信息*/
void students_shuchu()
{
int j,k;
printf("\n\n");
printf("--------------------------------------------------------------------\n");
printf("%10s%10s%8s%8s%8s%8s%8s%8s\n","学号","姓名","性别","成绩1","成绩2","成绩3","成绩4","成绩5");
printf("--------------------------------------------------------------------\n");
for(j=0;j<i;j++)
{
printf("%10d%10s%8s",students[j].num,students[j].name,students[j].sex);
for(k=0;k<M;k++)
printf("%8.2f",students[j].mark[k]);
printf("\n");
}
printf("--------------------------------------------------------------------\n");
printf("\n\n");
system("pause");
}
/*删除学生信息*/
void students_delete()
{
int j,k;
int del;
char del1[50];
printf("请输入学号:");
scanf("%d",&del);
printf("请输入姓名:");
scanf("%s",del1); //-----------------------
for(j=0;j<i;j++) //-------------注意数据个数
if(del==students[j].num)
{
if(strcmp((const char *)del1,students[j].name)==0) //验证学号以及姓名
{
printf("学生信息核对成功!\n");
for(k=j;k<i-1;k++) //-------------1)注意数据个数,2)结构体变量可以整体赋值
{
students[k]=students[k+1];
}
printf("该学生已删除!\n");
i--;
}
}
else
{
printf("学生信息核对出错或不存在!\n");
break;
}
system("pause");
}
/*读取文件*/
void read_txt()
{
FILE *fp;
int t;
if((fp=fopen("message.txt","rb"))==NULL)
{
printf("不能打开文件!\n");
return;
}
if(fread(&i,sizeof(int),1,fp)!=1) //------数据个数先读入变量i,fwrite()是写函数
printf("读文件错误!\n");
for(t=0;t<i;t++)
{
if (fread(&students[t],sizeof(struct stu),1,fp)!=1) //------读数据入数组,fwrite()是写函数。如果是文本文件对应fprintf()用fscanf()比较方便
printf("读文件错误!\n");
}
fclose(fp);
}
/*写入文件*/
void write_txt()
{
FILE *fp;
int t;
if((fp=fopen("message.txt","wb"))==NULL) //----操作单一不容易错,况且后面不涉及读操作----原先打开方式是文本格式
{
printf("不能打开文件!\n");
return;
}
if(fwrite(&i,sizeof(int),1,fp)!=1) //数据个数先写入文件,-----但是写入又是按二进制格式,如果是文本文件用fprintf()比较方便
printf("写入文件错误!\n");
for(t=0;t<i;t++)
{
if(fwrite(&students[t],sizeof(struct stu),1,fp)!=1)
printf("写入文件错误!\n");
}
fclose(fp);
}
/*主函数*/
void xxx()
{
int choice;
read_txt();
while(1)
{
system("cls");
printf("欢迎进入学生管理系统!\n");
printf("---------------------\n");
printf(" 1.添加学生信息\n");
printf(" 2.修改学生信息\n");
printf(" 3.查询学生信息\n");
printf(" 4.输出学生信息\n");
printf(" 5.删除学生信息\n");
printf(" 6.修改系统密码\n");
printf(" 7.退出学生系统\n");
printf("---------------------\n");
printf("请选择1-7:");
scanf("%d",&choice);
switch(choice)
{
case 1:
students_message();
break;
case 2:
students_recover();
break;
case 3:
students_chaxun1();
break;
case 4:
students_shuchu();
break;
case 5:
students_delete();
break;
case 6:
xiugai();
break;
case 7:
printf("已成功退出学生管理系统\n");
exit(0);
break;
default:
printf("错误\n");
getchar();
system("pause");
}
write_txt();
}
}