当前位置: 代码迷 >> C语言 >> [求助] 怎么又是乱码啊?
  详细解决方案

[求助] 怎么又是乱码啊?

热度:146   发布时间:2007-10-10 17:39:45.0
[求助] 怎么又是乱码啊?
建立3个学生的数据,求平均分数,再将数据输入到磁盘文件.不知道怎么回事,又是乱码?救命啊?
#include<stdio.h>
struct tt
{
char name[10];
float score[3];
int num;
float avg;
}boy[3];
main()
{
FILE *fp;
float sum=0;
int i,j;
for(i=0;i<3;i++)
{
printf("please input the %d boy name\n",i);
scanf("%s",&boy[i].name);
printf("\nplease input the %d boy num\n",i);
scanf("%d",&boy[i].num);
for(j=0;j<3;j++)
{
printf("\nplease input the %d boy three of score\n",i);
scanf("%f",&boy[i].score[j]);
sum+=boy[i].score[j];
boy[i].avg=sum/3.0;
}
printf("the sum=%f\n",sum);
printf("the average score is %f\n",boy[i].avg);
sum=0;
boy[i].avg=0;
}
if((fp=fopen("c:\\sf","a"))==NULL)
{
printf("\ncan't open this file\n");
exit(0);
}
for(i=0;i<3;i++)
if(fwrite(&boy[i],sizeof(struct tt),1,fp)!=EOF);
fclose(fp);
system("pause");
}
----------------解决方案--------------------------------------------------------
刚开始学,帮不到你了.

----------------解决方案--------------------------------------------------------
我看了一下:
意见如下:
#include<stdio.h>
#include <stdlib.h>
struct tt
{
char name[10];
float score[3];
int num;
double avg;
}boy[3];
void main()
{
FILE *fp;
double sum=0;
int i,j;
for(i=0;i<3;i++)
{
printf("please input the %d boy name\n",i);
scanf("%s",&boy[i].name);
printf("\nplease input the %d boy num\n",i);
scanf("%d",&boy[i].num);
for(j=0;j<3;j++)
{
printf("\nplease input the %d boy three of score\n",i);
scanf("%f",&boy[i].score[j]);
sum+=boy[i].score[j];
boy[i].avg=sum/3.00;
}
printf("the sum=%f\n",sum);
printf("the average score is %f\n",boy[i].avg);
sum=0;
boy[i].avg=0;
}
if((fp=fopen("c:\\sf","a"))==NULL)
{
printf("\ncan't open this file\n");
exit(0);
}
for(i=0;i<3;i++)
if(fwrite(&boy[i],sizeof(struct tt),1,fp)!=EOF)
fclose(fp);
system("pause");
}
我想应该OK了吧。
----------------解决方案--------------------------------------------------------

用fprintf试试,也许fwrite不支持struct.


----------------解决方案--------------------------------------------------------
还是不行?还是乱码啊!!
----------------解决方案--------------------------------------------------------
#include<stdio.h>
struct tt
{
char name[10];
float score[3];
int num;
float avg;
}boy[3];
main()
{
FILE *fp;
float sum=0;
int i,j;
for(i=0;i<3;i++)
{
printf("please input the %d boy name\n",i);
scanf("%s",&boy[i].name);
printf("\nplease input the %d boy num\n",i);
scanf("%d",&boy[i].num);
for(j=0;j<3;j++)
{
printf("\nplease input the %d boy three of score\n",i);
scanf("%f",&boy[i].score[j]);
sum+=boy[i].score[j];
boy[i].avg=sum/3.0;
}
printf("the sum=%f\n",sum);
printf("the average score is %f\n",boy[i].avg);
sum=0;
boy[i].avg=0;
}
if((fp=fopen("c:\\sf","a"))==NULL)
{
printf("\ncan't open this file\n");
exit(0);
}
for(i=0;i<3;i++)
if(fprintf(fp,"%s %f %f %f %d %f",boy[i].name,boy[i].score[0],boy[i].score[1],boy[i].score[2] ,boy[i].num,boy[i].avg )!=EOF);
fclose(fp);
system("pause");
}
----------------解决方案--------------------------------------------------------
我觉得是int类型的数据写入文本文件时出现的乱码
----------------解决方案--------------------------------------------------------
  相关解决方案