如何从c ++中的文件读取double [英] How to read in a double from a file in c++

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

问题描述

如何从C ++中的文件读取双精度值?

How do you read in a double from a file in C++?

对于int,我知道你可以使用getline(),然后atoi,找到一个数组去双重函数。

For ints I know you can use the getline() and then atoi, but I am not finding an array to double function. What is available for reading in doubles, or converting a char array to a double?

推荐答案

可以使用流提取:

std::ifstream ifs(...);
double d;
ifs >> d;

这项工作规定,除了空格之外,流中的下一个数据应该是文本表示的两倍。

This work provided that other then whitespace, the next data in the stream should be a double in textual representation.

提取后,您可以检查信息流的状态,以查看是否有错误:

After the extraction, you can check the state of the stream to see if there were errors:

ifs >> d;
if (!ifs)
{
    // the double extraction failed
}

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

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