C!!我的C程序出什么问题了啊???

来源:百度知道 编辑:UC知道 时间:2024/06/04 03:59:20
#include<stdio.h>
#include<stdlib.h>
#define N 2
struct student
{
int mun;
char name[20];
float score[3];
}str[N];

void main()
{
FILE *fp;
int i,j;

for(i=0;i<N;i++)
{
printf("input a student%d data:\n",i+1);
printf("NO:");
scanf("%d",&str[i].mun);
printf("name:");
scanf("%s",str[i].name);
for(j=0;j<3;j++)
{
printf("score%d:",j+1);
scanf("%f",&str[i].score[j]);
}
}
if((fp=fopen("kobe","w"))==NULL)
{
printf("error!\n");
exit(0);
}
for(i=0;i<N;i++)
fwrite(str,sizeof(struct student),1,fp);
fclose(fp);
}
谁帮我调试一下这程序 为什么我把变量 float score[3] 改成 int score[3] 在把语句scanf("%f",&str[i].score[j]); 改成scanf("%d",&str[i].score[j]); 程序就不会出问题

这样试试:
for(j=0;j<3;j++)
{
float temp;
printf("score%d:",j+1);
scanf("%f",&temp);
str[i].score[j]=temp;
}

&str[i].score[j]

改成

&(str[i].score[j])

试试

浮点数应该不能被写入2进制文件吧? 这只是我自己的猜想 不知道对不对