当前位置: 代码迷 >> C语言 >> 【求助】文件写入有问题,请求帮助
  详细解决方案

【求助】文件写入有问题,请求帮助

热度:348   发布时间:2007-05-20 11:11:13.0
【求助】文件写入有问题,请求帮助

#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

搜索更多相关的解决方案: 文件  请求  

----------------解决方案--------------------------------------------------------
for(i=0;i<N;i++);

----------------解决方案--------------------------------------------------------

这句有什么问题呢?


----------------解决方案--------------------------------------------------------

当然有错误,看看这段代码
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;
}

这个函数都没有write()写进文件,而且你返回的值是0,那么
if(writefile())
printf("The file successed......\n");
else
printf("The file unsuccessed......\n");
无论怎样都是打印else分支


----------------解决方案--------------------------------------------------------
以下是引用风间小艺在2007-5-20 23:01:22的发言:

这句有什么问题呢?

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);

看出来什么原因没有?


----------------解决方案--------------------------------------------------------

----------------解决方案--------------------------------------------------------
  相关解决方案