如何读取数据? [英] how to read data?

查看:60
本文介绍了如何读取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要读取浮点型数据,我们可以使用:


....

浮动f;

fscanf( outfile,%f,& f);

....


但是对于double类型,怎么做?

....

double d;

fscanf(outfile,"%f",& d);

....


这是否意味着我丢失了数据精确度?

to read float type data, we can use:

....
float f;
fscanf(outfile, "%f", &f);
....

but for double type, how to do that?
....
double d;
fscanf(outfile, "%f", &d);
....

Does that mean I lost data precision?

推荐答案

kathy写道:
读取浮点型数据,我们可以使用:

....
浮点数f;
fscanf(outfile," %f",& f);
....

但是对于双重型,怎么做?
....
双d;
fscanf(outfile,"%f",& d);
....

这是否意味着我丢失了数据精度?
to read float type data, we can use:

....
float f;
fscanf(outfile, "%f", &f);
....

but for double type, how to do that?
....
double d;
fscanf(outfile, "%f", &d);
....

Does that mean I lost data precision?


用C ++做:


加倍;


std :: cin>> d;


-

Ian Collins。


Do it in C++:

double d;

std::cin >> d;

--
Ian Collins.




"凯西" < YQ ***** @ yahoo.com>在消息中写道

news:11 ********************** @ o13g2000cwo.googlegr oups.com ...

"kathy" <yq*****@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
读取浮点型数据,我们可以使用:

浮动f;
fscanf(outfile,"%f",& f) ;
...

但对于双重类型,怎么做?
...
双d;
fscanf(outfile," %f,& d);


fscanf(outfile,"%lf",& d); ...

这是否意味着我丢失了数据精度?
to read float type data, we can use:

...
float f;
fscanf(outfile, "%f", &f);
...

but for double type, how to do that?
...
double d;
fscanf(outfile, "%f", &d);
fscanf(outfile, "%lf", &d); ...

Does that mean I lost data precision?




使用"%f"用''fscanf()''代表''double''

意味着你得到了不确定的行为。


BTW你考虑过使用std :: cin

而不是FILE *流?它已经为它定义了超载

运营商;

右键的类型会自动调用。


float f;

double d;


std :: cin>> F; //读取浮点数

std :: cin>> d; //读一个双子


-Mike



Using "%f" with ''fscanf()'' for type ''double''
means you get undefined behavior.

BTW have you considered using std::cin
instead of a FILE* stream? It has overloaded
operators defined for it; the one for the
right type gets called automatically.

float f;
double d;

std::cin >> f; // reads a float
std::cin >> d; // reads a double

-Mike


在C中,你可以这样做:

//作为字符串读取

char d [40];

fscanf(文件,%s,d);

///使用strtod将其转换为double()

调用strtod()

In C, you can do it like:
//read as a string
char d[40];
fscanf(file,"%s",d);
///convert it into double using strtod()
call strtod()


这篇关于如何读取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆