代码不多,大家帮忙看看fun()函数那里里应该怎么改,就是输不出正确的答案
#include <stdio.h>#define SIZE 10
struct student
{
int name[20];
char num;
float score1; /*高数成绩*/
float creh1; /*高数学分*/
float score2; /*英语成绩*/
float creh2; /*英语学分*/
float score3; /*物理成绩*/
float creh3; /*物理学分*/
}stu[SIZE];
/****************************************************************************/
void save()
{FILE *fp;
int i;
if ((fp=fopen(“C:\\c\\student.txt”,”rb”))==NULL)
{printf(“can not open file\n”);
exit(0);
}
for(i=0;i<SIZE;i++)
if(fwrite(stu[i],sizeof(struct student),1,fp))!=1)
{printf(“file write error\n”);
fclose(fp);
}
}
/****************************************************************************/
void menu() /*主菜单界面*/
{
printf(“\t\t********************************************\n\n”);
printf(“\t\t********************************************\n\n”);
printf(“\t\t ***choose function****************************\n\n”);
printf(“\t\t ***1 search**********************************\n\n”);
ptintf(“\t\t ***2 backupdata******************************\n\n”);
ptintf(“\t\t ***3 exit************************************\n\n”);
printf(“\t\t********************************************\n\n”);
printf(“\t\t********************************************\n\n”);
}
/*******************************************************************/
fun()
{int i;
FILE *fp,*fp1;
fp1= fopen(“C:\\c\\student.txt”,”rb”); /*打开student.txt文件*/
if((fp1==NULL)
{printf(“can not open file\n”);
return;
}
for(i=0;i<SIZE;i++)
{if(fread(&stu[i],sizeof(struct student),1,fp))!=1) /*从student.txt文件中读取数据*/
{if (feof(fp1)){fclose(fp1);return;}
printf(“file reas error\n”);
}
}
for(i=0;i<SIZE;i++)
{if(stu[i].creh1==0 || stu[i].creh2==0||stu[i].creh3==0) /*三科是否有一科不及格*/
{printf(“%s was fail!\n”,stu[i].name); /*输出谁不及格*/
}
else
{printf(“%s has pass!\n”stu[i].name); /*输出及格的人*/
if((fp=fopen(“C:\\c\\score.txt”,”wb”))==NULL) /*打开score.txt文件*/
{printf(“can not open file\n”);
return;
}
if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1) /*向score.txt文件中输入数据*/
printf(“file write error\n”);
}
}
fclose(fp)
}
/**********************************************************************/
backupdata()
{int i;
FILE *in,*out;
if ((out=fopen(“C:\\c\\student.txt”,”rb”))==NULL) /*打开student.txt文件*/
{printf(“can not open outfile\n”);
exit(0);
}
for(i=0;i<SIZE;i++)
{if(fread(&stu[i],sizeof(struct student),1,out)!=1) /*从student.txt读取数据*/
printf(“file read error\n”);
}
fclose(out);
if((in=fopen(“C:\\c\\backup.txt”,”wb”))==NULL) /*打开backup.txt*/
{printf(“can not open infile\n”);
exit(0);
}
for(i=0;i<10;i++)
{ if(fwrite(&stu[i],sizeof(struct student),1,in)!=1) /*将student.txt的数据写
printf(“file write error\n”); 入backup.txt中*/
}
fclose(in);
}
/*****************************************************************************/
void main()
{FILE *fp;
char build,other=’y’;
int i,n,wl;
fp=fopen(“C:\\c\\student.txt”,”rb”);
if(fp==NULL)
{system(“cls”);
printf(“\n====>the file is inexistence,build?(y/n)\n”); /*没有student.txt文件
scanf(“%c”,&build); 是否创建*/
if(build==’y’||build==’Y’)
{fp=fopen(“C:\\c\\student.txt”,”wb”);
printf(“please enter name,number,GS,GSXF,YY,YYXF,WL,WLXF\n”);
for(i=0;i<SIZE;i++)
scanf(“%s,%d,%f,%f,%f,%f,%f,%f”,stu[i].name,stu[i].num,stu[i].score1,stu[i].creh1, stu[i].score2,stu[i].creh2, stu[i].score3,stu[i].creh3); /*输入每个学生的数据*/
save(); /*保存到student.txt中*/
}
else
exit(0);
}
while(other==’y’||other==’Y’)
{
system(“cls”);
menu();
scanf(“%d”,&n);
if(n>=1&&n>=3)
{switch(n)
{case 1:fun();break;
case 2:backupdata();break;
case 3:exit(0);
}
}
else exit(0);
system(“pause”);
printf(“is there anything else?(y/n)”); /*是否有其它事?*/
scanf(“%c”,&other);
}
[[it] 本帖最后由 saysaysay 于 2008-6-26 23:42 编辑 [/it]]
[[it] 本帖最后由 saysaysay 于 2008-6-26 23:43 编辑 [/it]]