count是在程序开始赋值为0的,count是表示输入的条数,是全局变量。count始终是0,为什么不自增?
void input() //录入数据
{
FILE * fp;
if((fp=fopen("stu.dat","rb"))==NULL)
{
fp=fopen("stu.dat","w");
}
else
{
fp=fopen("stu.dat","ab");
}
system("cls");
do
{
printf("\n\t\t\t\t学生信息录入\n");
printstar();
printf("\n学号:");
scanf("%d",&student1.ID);
printf("\n姓名:");
scanf("%s",&student1.name);
printf("\n三门课成绩分别为:");
scanf("%lf %lf %lf",&student1.score1,&student1.score2,&student1.score3);
student1.average=(student1.score1+student1.score2+student1.score3)/3;
printf("\n平均成绩为:%3.1lf\n",student1.average);
fwrite(&student1,sizeof(struct student),1,fp);
count++;
fflush(stdin);
printf("是否继续录入(y/n)?");
scanf("%c",&choice);
system("cls");
if(choice!='y'&&choice!='n')
{
printstar();
printf("\n\t您输入了非法字符,请重新输入!\n\n");
printstar();
time();
}
printf("\n");
}while(choice=='y');
fclose(fp);
}
void browse() //浏览
{
system("cls");
FILE * fp;
int i;
fp=fopen("stu.dat","rb");
printf("\n\t\t\t\t学生信息浏览\n");
for(i=0;i<count;i++)
{
fread(&student1,sizeof(struct student),1,fp);
printf("\t学号 姓名 成绩1 成绩2 成绩3 平均成绩\n");
printf("\t%d %s %4.1lf %4.1lf %4.1lf %4.1lf\n",student1.ID,student1.name,student1.score1,student1.score2,student1.score3,student1.average);
}
}
因为count的值始终为0,而i的初始值也为0,所以在浏览函数里如果用for语句,那么输出的就是一句“Press any key to continue”,呵呵。如果浏览函数里不用for语句,输出时将显示一条信息,而事实上在文件里不只有一条信息。请看出错误的地方的人回答啊。
3 个解决方案
#1
?
#2
lz有没结贴的前科~本人闪了~
#3
呵呵
#1
?
#2
lz有没结贴的前科~本人闪了~
#3
呵呵