| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 359 人关注过本帖
标题:[求助] 怎么又是乱码啊?
只看楼主 加入收藏
cutboy
Rank: 1
等 级:新手上路
帖 子:120
专家分:0
注 册:2007-9-16
收藏
 问题点数:0 回复次数:6 
[求助] 怎么又是乱码啊?
建立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");
}
2007-10-10 17:39
endaps
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2007-10-5
收藏
得分:0 
刚开始学,帮不到你了.
2007-10-10 18:04
pinglideyu
Rank: 3Rank: 3
来 自:武汉工程大学
等 级:论坛游侠
威 望:1
帖 子:735
专家分:140
注 册:2007-1-7
收藏
得分:0 
我看了一下:
意见如下:
#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了吧。

~~我的明天我知道~~
2007-10-10 18:14
vbc
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:352
专家分:147
注 册:2006-12-30
收藏
得分:0 

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


清远鹏程万里人才网:[url=http://www.]http://www.[/url]zq.,qy.
2007-10-10 18:39
cutboy
Rank: 1
等 级:新手上路
帖 子:120
专家分:0
注 册:2007-9-16
收藏
得分:0 
还是不行?还是乱码啊!!

2007-10-10 18:51
vbc
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:352
专家分:147
注 册:2006-12-30
收藏
得分:0 
#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");
}

清远鹏程万里人才网:[url=http://www.]http://www.[/url]zq.,qy.
2007-10-10 19:31
静思
Rank: 3Rank: 3
来 自:沈阳
等 级:新手上路
威 望:8
帖 子:630
专家分:0
注 册:2006-2-28
收藏
得分:0 
我觉得是int类型的数据写入文本文件时出现的乱码

英者自知,雄者自胜
2007-10-10 19:35
快速回复:[求助] 怎么又是乱码啊?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012570 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved