C 一个烫烫烫的问题

Scorpion_xyt 2018-09-07 08:44:35
#include <stdio.h>
#include <memory.h>
struct student
{
int num;
char name[10];
float score1;
float score2;
float score3;
float aver;
};
main()
{
int i,nu;
float sc1,sc2,sc3;
char na[10];
struct student st[1];
FILE *fp;
if((fp=fopen("E:\student.txt","w+"))==NULL)
{
printf("can not open file\n");
exit(0);
}
setbuf(fp,NULL);
for(i=0;i<1;i++)
{
scanf("%d%s%f%f%f",&nu,&na,&sc1,&sc2,&sc3);
fprintf(fp,"%d %s %f %f %f %f",nu,na,sc1,sc2,sc3,(sc1,sc2,sc3)/3);
fflush(fp);
}
for(i=0;i<1;i++)
{
fscanf(fp,"%d%s%f%f%f%f%f",&st[i].num,&st[i].name,&st[i].score1,&st[i].score2,&st[i].score3,&st[i].aver);
}
for(i=0;i<1;i++)
{
printf("%d %s %.2f %.2f %.2f %.2f",st[i].num,st[i].name,st[i].score1,st[i].score2,st[i].score3,st[i].aver);
}
fclose(fp);
}



请求指点,为什么输出的数据都不正确?应该怎么解决呢?
...全文
262 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
自信男孩 2018-09-08
  • 打赏
  • 举报
回复
加一个rewind(fp);或者fseek(fp, 0, SEEK_SET);
zhangchm2018 2018-09-08
  • 打赏
  • 举报
回复
引用 2 楼 zhangchm2018 的回复:
[quote=引用 1 楼 zhangchm2018 的回复:]
因为 fprintf 和 fscanf 输入输出的只能是 char 类型的数据,所以它会将你输入的任意数据都转换成 char 类型的
如果你想输入和读取 int, float, double 之类的数据,推荐使用 fwrite 和 fread 函数
fread http://www.runoob.com/cprogramming/c-function-fread.html
fwrite http://www.runoob.com/cprogramming/c-function-fwrite.html
比如 float 类型的 88,会转换成 '8' '8'[/quote]3L说的对,是我错了
zhouqunhai 2018-09-08
  • 打赏
  • 举报
回复
感觉3楼回答正确
zangfong 2018-09-07
  • 打赏
  • 举报
回复
就你这个题目来说,是你的文件指针fp的位置问题引起的,如果要读取你刚刚写入student.txt里的那些数据,你需要把文件指针fp重新移到文件开头
#include <stdio.h>
#include <memory.h>
#include<stdlib.h>
struct student
{
int num;
char name[10];
float score1;
float score2;
float score3;
float aver;
};
main()
{
int i,nu;
float sc1,sc2,sc3;
char na[10];
struct student st[1];
FILE *fp;
if((fp=fopen("E:\student.txt","w+"))==NULL)
{
printf("can not open file\n");
exit(0);
}
setbuf(fp,NULL);
for(i=0; i<1; i++)
{
scanf("%d%s%f%f%f",&nu,&na,&sc1,&sc2,&sc3);
fprintf(fp,"%d %s %f %f %f %f",nu,na,sc1,sc2,sc3,(sc1,sc2,sc3)/3);
fflush(fp);
}

fseek(fp,0L,SEEK_SET); // 定位文件读写指针fp到文件开头

for(i=0; i<1; i++)
{
fscanf(fp,"%d%s%f%f%f%f%f",&st[i].num,&st[i].name,&st[i].score1,&st[i].score2,&st[i].score3,&st[i].aver);
}
for(i=0; i<1; i++)
{
printf("%d %s %.2f %.2f %.2f %.2f",st[i].num,st[i].name,st[i].score1,st[i].score2,st[i].score3,st[i].aver);
}
fclose(fp);
}
zhangchm2018 2018-09-07
  • 打赏
  • 举报
回复
引用 1 楼 zhangchm2018 的回复:
因为 fprintf 和 fscanf 输入输出的只能是 char 类型的数据,所以它会将你输入的任意数据都转换成 char 类型的
如果你想输入和读取 int, float, double 之类的数据,推荐使用 fwrite 和 fread 函数
fread http://www.runoob.com/cprogramming/c-function-fread.html
fwrite http://www.runoob.com/cprogramming/c-function-fwrite.html
比如 float 类型的 88,会转换成 '8' '8'
zhangchm2018 2018-09-07
  • 打赏
  • 举报
回复
因为 fprintf 和 fscanf 输入输出的只能是 char 类型的数据,所以它会将你输入的任意数据都转换成 char 类型的
如果你想输入和读取 int, float, double 之类的数据,推荐使用 fwrite 和 fread 函数
fread http://www.runoob.com/cprogramming/c-function-fread.html
fwrite http://www.runoob.com/cprogramming/c-function-fwrite.html

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧