这个作业让我头疼 C语言读取txt文件中的小数的问题

k854053320k08 2018-12-05 04:18:11

需要找出x ,y ,z,这3列的最大值,最小值,平均值,输出成txt文件
感觉无从下手············
...全文
425 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2018-12-05
  • 打赏
  • 举报
回复
请判断scanf函数的返回值。
//NAME: essaie bla bla
//DIMENSION: 8
//DATA
//1 14 15
//2 11 10
//3 6 4
//4 7 13
//5 9 21
//6 19 3
//7 1 5
//8 8 8
//EOF
//
// 文本文件中可能还含有其他内容,但是需要用到的内容即以上

//比如data.txt:
//NAME: essaie bla bla
//其它内容
//DIMENSION: 8
//其它内容
//DATA
//其它内容
//1 14 15
//其它内容
//2 11 10
//其它内容
//3 6 4
//其它内容
//4 7 13
//其它内容
//5 9 21
//其它内容
//6 19 3
//其它内容
//7 1 5
//其它内容
//8 8 8
//其它内容
//EOF

// 目标是要获取NAME后字串,DIMENSION后数值,以及DATA以下的数值
// 其中NAME就是随便个字句,DIMENSION是城市数量,DATA以下是城市编号,X坐标,Y坐标
// 所有的这些将赋值给一个事先定义好的结构
#include <stdio.h>
#include <string.h>
#define MAXCPL 80 //每行最大字符数
#define MAXCITY 100 //每组数据中DATA最多项数,DIMENSION的最大值
#define MAXNAMEL 32 //NAME最大长度
struct S {
char NAME[MAXNAMEL+1];
int DIMENSION;
struct D {
int NO;
int X;
int Y;
} DATA[MAXCITY];
} s;
FILE *f;
int st,n,i;
char ln[MAXCPL];
int main() {
f=fopen("data.txt","r");
if (NULL==f) {
printf("Can not open file data.txt!\n");
return 1;
}
st=0;
n=0;
while (1) {
if (NULL==fgets(ln,MAXCPL,f)) break;
if (st==0) {
if (1==sscanf(ln,"NAME: %31[^\n]",s.NAME)) st=1;
} else if (st==1) {
if (1==sscanf(ln,"DIMENSION: %d",&s.DIMENSION)) st=2;
} else if (st==2) {
if (0==strcmp(ln,"DATA\n")) st=3;
} else if (st==3) {
if (3==sscanf(ln,"%d%d%d",&s.DATA[n].NO,&s.DATA[n].X,&s.DATA[n].Y)) {
n++;
if (n>=MAXCITY || n>=s.DIMENSION) break;
}
}
}
fclose(f);
printf("s.NAME=[%s]\n",s.NAME);
printf("s.DIMENSION=%d\n",s.DIMENSION);
for (i=0;i<n;i++) {
printf("s.DATA[%d].NO,X,Y=%d,%d,%d\n",i,s.DATA[i].NO,s.DATA[i].X,s.DATA[i].Y);
}
return 0;
}
//s.NAME=[essaie bla bla]
//s.DIMENSION=8
//s.DATA[0].NO,X,Y=1,14,15
//s.DATA[1].NO,X,Y=2,11,10
//s.DATA[2].NO,X,Y=3,6,4
//s.DATA[3].NO,X,Y=4,7,13
//s.DATA[4].NO,X,Y=5,9,21
//s.DATA[5].NO,X,Y=6,19,3
//s.DATA[6].NO,X,Y=7,1,5
//s.DATA[7].NO,X,Y=8,8,8

@风轻云淡_ 2018-12-05
  • 打赏
  • 举报
回复
可以参考一下
#include <fstream>
#include <sstream>

using namespace std;

struct Score
{
int num;
float score_one;
float score_two;
float score_thr;
float score_max;
float score_min;
float score_avg;
};

void scoreCalculate(Score& mscore)
{
float min = mscore.score_one<mscore.score_two?mscore.score_one:mscore.score_two;
mscore.score_min = min<mscore.score_thr?min:mscore.score_thr;

float max = mscore.score_one>mscore.score_two?mscore.score_one:mscore.score_two;
mscore.score_max = max>mscore.score_thr?max:mscore.score_thr;

mscore.score_avg = (mscore.score_one+mscore.score_two+mscore.score_thr)/3;
}

int main()
{
fstream read_ptr("/root/123.txt",ios_base::binary|ios_base::in);
string one_row;
stringstream ss;
Score score;
bool has_head = true;//用于跳过数据头
while(getline(read_ptr,one_row))
{
if(has_head)
{
has_head = false;
continue;
}
sscanf(one_row.c_str(),"%d%f%f%f",&score.num,&score.score_one,&score.score_two,&score.score_thr);
scoreCalculate(score);
ss << score.score_max << " " << score.score_min << " " << score.score_avg << endl;
}
read_ptr.close();
read_ptr.open("/root/1234.txt",ios_base::binary|ios_base::out);
read_ptr << ss.str().c_str();
read_ptr.close();

return 0;
}
@风轻云淡_ 2018-12-05
  • 打赏
  • 举报
回复
getLine 一次取一行,sscanf进行格式化输入,然后求你想求的值。
康笨笨 2018-12-05
  • 打赏
  • 举报
回复
文件读取直接用呀,百度一下看看就懂啦
zhouqunhai 2018-12-05
  • 打赏
  • 举报
回复
直接%lf读取,有什么困难吗
自信男孩 2018-12-05
  • 打赏
  • 举报
回复

fgets(buf, 16, fp); //first line
fscanf(fp, "%d %lf %lf\n", &no, &f1, &f2, &f3);




建议使用结构体数组存放no, f1, f2, f3
636f6c696e 2018-12-05
  • 打赏
  • 举报
回复
readline split(' ')
英雄@末路 2018-12-05
  • 打赏
  • 举报
回复
格式化输入fscanf, 空格会作为分隔符处理。

69,373

社区成员

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

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