用什么可以代替fwriter向文件中输入结构体数据
#include<stdio.h>#include<stdlib.h>
#include<string.h>
#define N 7
struct yaoming //声明结构体yaoming
{
char season[10];
int GP;
float time;
float rebound;
float assist;
float block_shot;
float foul;
float score;
}yao[N],yao_result; //定义结构体变量,存放赛季数据的yao[N]和存放综合数据的yao_result
void save() //定义函数save,将输入的赛季数据存放至数据文件yao.dat中
{
FILE *fp;
int i;
if((fp=fopen("yaoming.dat","wb"))==NULL)
{
printf("cannot open file\n");
return;
}
for(i=0;i<N;i++)
{if(fwrite(&yao[i],sizeof(struct yaoming),1,fp)!=1)
printf("file write error\n");}
if(fwrite(&yao_result,sizeof(struct yaoming),1,fp)!=1)
printf("file write error\n");
fclose(fp);
}
void print() //定义函数print,将文件中的数据打印出来
{
FILE *fp;
int i;
if((fp=fopen("yaoming.dat","rb"))==NULL)
{
printf("cannot open file\n");
exit(0);
}
for(i=0;i<N;i++)
{
fread(&yao[i],sizeof(struct yaoming),1,fp);
printf("%s\t%d\t%f\t%f\t%f\t%f\t%f\t%f\n",yao[i].season,yao[i].GP,yao[i].time,yao[i].rebound,yao[i].assist,yao[i].block_shot,yao[i].foul,yao[i].score);
}
fread(&yao_result,sizeof(struct yaoming),1,fp);
printf("%s\t%d\t%f\t%f\t%f\t%f\t%f\t%f\n",yao_result.season,yao_result.GP,yao_result.GP,yao_result.time,yao_result.rebound,yao_result.assist,yao_result.block_shot,yao_result.foul,yao_result.score);
fclose(fp);
}
int main()
{
int i,j,a=0;
float b=0.0,c=0.0,d=0.0,e=0.0,m=0.0,n=0.0;
char t;
printf("please enter data of yaoming:\n");
for(i=0;i<N;i++) //输入姚明的赛季数据
scanf("%s%d%f%f%f%f%f%f",yao[i].season,&yao[i].GP,&yao[i].time,&yao[i].rebound,&yao[i].assist,&yao[i].block_shot,&yao[i].foul,&yao[i].score);
for(j=0;j<N;j++) //计算得出综合数据
{
a+=yao[i].GP;
b+=yao[i].time;
c+=yao[i].rebound*yao[i].time;
d+=yao[i].assist*yao[i].time;
e+=yao[i].block_shot*yao[i].time;
m+=yao[i].foul*yao[i].time;
n+=yao[i].score*yao[i].time;
}
t=(char)N;
yao_result.season[0]=t;
yao_result.GP=a;
yao_result.time=b/N;
yao_result.rebound=c/a;
yao_result.assist=d/a;
yao_result.block_shot=e/a;
yao_result.foul=m/a;
yao_result.score=n/a;
save(); //调用save函数
print(); //调用print函数
return 0;
}
这里我才输入一行数据: 08-09 77 33.6 9.9 1.8 1.9 3.3 19.7 (回车)
然后就显示:scanf:floating point formats not linked
Abnormal program termination
有人说用fwrite怎么也调试不对的,为什么呢?
这里fwrite如何用scanf代替呢?