C程序输出错误!谁帮我看下!谢谢

来源:百度知道 编辑:UC知道 时间:2024/05/30 05:34:17
#include<stdio.h>
#include<stdlib.h>
#define N 2
struct student
{
int mun;
char name[20];
float score[3];
float ave;
}str[N],boy[N];

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

for(i=0;i<N;i++) //输入N个学生的数据 并求出平均分ave
{
k=0;
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]);
k+=str[i].score[j];
}
str[i].ave=k/3;
}

if((fp=fopen("kobe","w+"))==NULL)
{
printf("error!\n");
exit(0);
}

fwrite(str,sizeof(struct student),N,fp);//将str[N]的数据写入文件kobe
rewind(fp); //返回文件位置指针

fread(boy,sizeo

按你的程序我这里没问题啊!?
不过你的程序有个地方有错:
scanf("%s",&str[i].name);
str[i].name本来就是一个地址,就不必再加&了,
改为scanf("%s",str[i].name); 即可!
你试试是不是这个引起的问题!

程序没有错,我的就可以运行,按头文件来看,你用的应该是C语言,你要 是用C++一点问题都没有了

#include<stdio.h>
#include<stdlib.h>
#define N 2
struct student
{
int mun;
char name[20];
float score[3];
float ave;
}str[N];

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

for(i=0;i<N;i++) //输入N个学生的数据 并求出平均分ave
{
k=0;
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]);
k+=str[i].score[j];
}
str[i].ave=k/3;
}

if((fp=fopen(&q