#include "stdio.h"
typedef struct
{
char name[100];
double height;
double weight;
}stu;
int main(void)
{
FILE *fp;
int i;
stu std[6];
//int number=6; //没有使用
int ret;
if ((fp = fopen("hw.dat", "r")) == NULL) /* 打开文件 */
printf("\n文件打开失败。\n");
else
{
i=0;
while(feof(fp) == 0) //还没有达到文件结尾,就一直读取数据
{
ret=fscanf(fp,"%s%lf%lf",std[i].name,&std[i].height,&std[i].weight);
if(ret==3)
{
printf("%-10s %5.1f %5.1f\n",std[i].name,std[i].height,std[i].weight);
i++;
}
}
//原代码
/*
i=0;
while ( fscanf(fp,"%s%lf%lf",std[i].name,&std[i].height,&std[i].weight)==3)
{
printf("%-10s %5.1f %5.1f\n",std[i]); i++;
*/
}
return 0;
}