读文件时出错。大家帮忙修改下吧
在写入考试成绩后,重新从本地读取出错。不知道哪里错了。程序代码:
#include "stdio.h" #include "time.h" #include "stdlib.h" void main() { int caozuo,weishu=0,tishu=0,zongfen=0,suanfa=0,i,shu1,shu2,a,hd,da,ii,fp; int plus(int tishu,int a); int minus(int tishu,int a); int multiply(int tishu,int a); int devide(int tishu,int a); int random(int tishu,int a); void ceyan(int tishu,int a,int zongfen); void chakan(); srand((unsigned)time(NULL)); loop: printf("\n\n\t\t☆★☆★☆★☆★☆★☆★☆★☆★☆★\n"); printf("\t\t★ ☆\n"); printf("\t\t☆ 算术练习 ★\n"); printf("\t\t★ 1 练习 ☆\n"); printf("\t\t☆ 2 测验 ★\n"); printf("\t\t★ 3 设置 ☆\n"); printf("\t\t☆ 4 查看测验成绩 ★\n"); printf("\t\t★ 0 退出 ☆\n"); printf("\t\t☆ ★\n"); printf("\t\t★☆★☆★☆★☆★☆★☆★☆★☆★☆") ; printf("\n\n请输入您要进行的操作:"); scanf("%d",&caozuo); if((caozuo!=1)&&(caozuo!=2)&&(caozuo!=3)&&(caozuo!=4)&&(caozuo!=0)) { printf("\n提示:\n输入有误,请重新操作\n\n\n"); goto loop; } system("cls"); switch(caozuo) { case 1 : { if(tishu==0) { system("cls"); printf("\n请先设置再进入练习模式!\n按任意键返回主菜单。"); getchar(); getchar(); system("cls"); goto loop; } switch(suanfa) { case 1:ii=plus(tishu,a);break; case 2:ii=minus(tishu,a);break; case 3:ii=multiply(tishu,a);break; case 4:ii=devide(tishu,a);break; case 5:ii=random(tishu,a);break; } printf("总答题数:%d\n答对%d题\n正确率%.2f%%\n得分:%.1f ",tishu,ii,ii*100.0/tishu,zongfen*1.0/tishu*ii); getchar();} break; case 2: if(tishu==0) { system("cls"); printf("\n请先设置再进行测验模式!\n按任意键返回主菜单。"); getchar(); getchar(); system("cls"); goto loop; } else ceyan(tishu,a,zongfen); break; case 3: printf("设置:\n"); chongf1: printf("\n数据位数(1~3):\n"); scanf("%d",&weishu); if((weishu<1)||(weishu>3)) { printf("超出数据范围!\n"); goto chongf1; } switch(weishu) { case 1:a=9;break; case 2:a=99;break; case 3:a=999;break; default:; } chongf2: printf("\n测试模式题数:\n"); scanf("%d",&tishu); if(tishu<0) { printf("题数输入错误!\n"); goto chongf2; } chongf3: printf("\n测试模式总分:\n"); scanf("%d",&zongfen); if(zongfen<0) { printf("总分输入错误!\n"); goto chongf3; } chongfu4: printf("\n练习模式运算符为:1.加法 2.减法 3.乘法 4.除法 5.随机\n"); printf("(测验模式运算符默认随机)\n"); scanf("%d",&suanfa); if(suanfa<1||suanfa>5) { printf("运算输入错误"); goto chongfu4; } printf("========================\n"); printf("数据位数:%d\n",weishu); printf("总题数:%d\n",tishu); printf("总分:%d\n",zongfen); switch(suanfa) { case 1:printf("运算符号:+");break; case 2:printf("运算符号:-");break; case 3:printf("运算符号:*");break; case 4:printf("运算符号:/");break; case 5:printf("运算符号:随机");break; } getchar(); getchar(); system("cls"); goto loop; case 4 :chakan(); case 0 :exit(0); } } int plus(int tishu,int a) { int hd,da,shu1,shu2,i,ii=0; for(i=1;i<=tishu;i++) { shu1=rand()%a; shu2=rand()%a; da=shu1+shu2; printf("%d) %d+%d=",i,shu1,shu2); scanf("%d",&hd); if(hd==da) { printf("√\n\n"); ii++; } else printf("×正确答案:%d\n\n",da); } return (ii); } int minus(int tishu,int a) { int hd,da,shu1,shu2,i,ii=0; for(i=1;i<=tishu;i++) {shu1=rand()%a; shu2=rand()%a; da=shu1-shu2; printf("%d) %d-%d=",i,shu1,shu2); scanf("%d",&hd); if(hd==da) { printf("√\n\n"); ii++; } else printf("×正确答案:%d\n\n",da);} return (ii); } int multiply(int tishu,int a) { int hd,da,shu1,shu2,i,ii=0; for(i=1;i<=tishu;i++) { shu1=rand()%a; shu2=rand()%a; da=shu1*shu2; printf("%d) %d*%d=",i,shu1,shu2); scanf("%d",&hd); if(hd==da) { printf("√\n\n"); ii++; } else printf("×正确答案:%d\n\n",da);} return (ii); } int devide(int tishu,int a) { int shu1,shu2,i,ii=0; int check(int A,int B); float hd,da; for(i=1;i<=tishu;i++) {loop: shu1=rand()%a; shu2=rand()%a; if(check(shu1,shu2)==-1||shu2==0) goto loop; da=shu1*1.0/shu2; printf("%d) %d/%d=",i,shu1,shu2); scanf("%f",&hd); if(hd==da) { printf("√\n\n"); ii++; } else printf("×正确答案:%d\n\n",da);} return (ii); } int check(int A,int B) { float temp; temp=(float)A/(float)B; if(temp*B==(float)A){ return 1; } return -1; } int random(int tishu,int a) { int check(int A,int B); int hd,da,shu1,shu2,i,ii=0,b; for(i=1;i<=tishu;i++) { b=rand()%4; loop: shu1=rand()%a; shu2=rand()%a; switch(b) { case 1: {da=shu1+shu2; printf("%d) %d+%d=",i,shu1,shu2); scanf("%d",&hd); if(hd==da) {printf("√\n\n"); ii++;} else printf("× 正确答案%d\n\n",da);} break; case 2: {da=shu1-shu2; printf("%d) %d-%d=",i,shu1,shu2); scanf("%d",&hd); if(hd==da) printf("√\n\n"); else printf("× 正确答案%d\n\n",da);} break; case 3: {da=shu1*shu2; printf("%d) %d*%d=",i,shu1,shu2); scanf("%d",&hd); if(hd==da) printf("√\n\n"); else printf("× 正确答案%d\n\n",da);} break; case 4: {if(check(shu1,shu2)==-1||shu2==0) goto loop; da=shu1*1.0/shu2; printf("%d) %d/%d=",i,shu1,shu2); scanf("%f",&hd); if(hd==da) { printf("√\n\n"); ii++; } else printf("×正确答案:%d\n\n",da);} } } return(ii); } void ceyan(int tishu,int a,int zongfen) { struct cysj { char name[10]; int daan[tishu]; int fs; }cysj; int hd,da,shu1,shu2,i,ii=0,b; int check(int A, int B); printf("测验:\n"); printf("姓名:"); scanf("%s",&cysj.name); for(i=1;i<=tishu;i++) { b=rand()%3+1; loop1: shu1=rand()%a; shu2=rand()%a; switch(b) { case 1: {da=shu1+shu2; printf("%d) %d+%d=",i,shu1,shu2); scanf("%d",&hd); cysj.daan[i-1]=da; if(hd==da) ii++; } break; case 2: {da=shu1-shu2; printf("%d) %d-%d=",i,shu1,shu2); scanf("%d",&hd); cysj.daan[i-1]=da; if(hd==da) ii++; } break; case 3: {da=shu1*shu2; printf("%d) %d*%d=",i,shu1,shu2); scanf("%d",&hd); cysj.daan[i-1]=da; if(hd==da) ii++; } break; case 4: if(check(shu1,shu2)==-1||shu2==0) goto loop1; da=shu1*1.0/shu2; printf("%d) %d/%d=",i,shu1,shu2); scanf("%f",&hd); cysj.daan[i-1]=da; if(hd==da) ii++; break; } } printf("☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★\n"); printf("\t\t测试结束\n"); printf("\t按任意键查看本次测试成绩与答案\n"); printf("☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★\n"); getchar(); getchar(); printf("总题数:%d\n正确题数:%d\n得分:%.1f\n",tishu,ii,zongfen*1.0/tishu*ii); printf("答案:\n"); for(i=0;i<tishu;i++) printf("%d\n",cysj.daan[i]); printf("可在主菜单查看功能中查看本次测验成绩"); getchar(); FILE *fp; fp=fopen("D:\\测验结果.dat","w+"); fwrite(cysj.name,sizeof(char),1,fp); fprintf(fp,"\n答案:\n"); fwrite(cysj.daan,sizeof(int),tishu,fp); fprintf(fp,"\n总题数:%d\n",tishu); fprintf(fp,"\n答对%d题\n",ii); fprintf(fp,"\n得分:%.1f\n",zongfen*1.0/tishu*ii); fclose(fp); } void chakan() { int ch=0; FILE *fp; fp=fopen("D:\\测验结果.dat","r"); if(fp==NULL) { printf("\ncannot open the file"); exit(1); } else { while((ch=fgetc(fp))!=EOF) { putchar(ch); } } fclose(fp); }