#include <stdio.h>
#define N 5
struct info
{
int id;
char name[20];
float score[3];
float max;
float min;
}stu[N];
float high(float a[])
{
float max=0.0;
int i;
for(i=0;i<3;i++)
if(a[i]>max)
max=a[i];
return max;
}
float low(float a[])
{
float min;
int i;
min=a[0];
for(i=1;i<3;i++)
if(a[i]<min)
min=a[i];
return min;
}
int writefile()
{
FILE *fp;
int i;
if((fp=fopen("StudentScore.txt","w+"))==NULL)
return -1;
else
for(i=0;i<N;i++);
fprintf(fp,"%d %s %.1f %.1f %.1f %.1f %.1f\n",stu[i].id,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].max,stu[i].min);
fclose(fp);
return 0;
}
void main()
{
int i,j;
for(i=0;i<N;i++)
{
printf("Please input the %d student information:\n",i+1);
while (1)
{
printf("student number<101~999>:");
scanf("%d",&stu[i].id);
getchar();
if(stu[i].id>100 && stu[i].id<1000)
break;
else
printf("The number is wrong.Please repeat now.\n");
}
printf("student name:");
gets(stu[i].name);
for(j=0;j<3;j++)
while(1)
{
printf("the %d subject score(0~100):",j+1);
scanf("%f",&stu[i].score[j]);
getchar();
if (stu[i].score[j]>=0.0 && stu[i].score[j]<=100.0)
break;
else
printf("The number is wrong.Please repeat now.\n");
}
}
printf("-------------------------------------------------------------------------\n");
for(i=0;i<N;i++)
{
printf("The %d student information:\n",i+1);
printf("\tstudent-no:%d\n",stu[i].id);
printf("\tstudent-name:%s\n",stu[i].name);
for(j=0;j<3;j++)
printf("\tthe subject %d score:%.1f\n",i+1,stu[i].score[j]);
stu[i].max=high(stu[i].score);
stu[i].min=low(stu[i].score);
printf("\n\tthe hightest score is %.1f\n",stu[i].max);
printf("\tthe lowest score is %.1f\n\n",stu[i].min);
}
if(writefile())
printf("The file successed......\n");
else
printf("The file unsuccessed......\n");
}
写文件的子程序好像有问题,运行的结果写不到文件里面,请高手帮忙看看
应该是把5条记录都写到文件里,怎么就写了一行0