Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何检测atof或wtof故障?_C++ - Fatal编程技术网

C++ 如何检测atof或wtof故障?

C++ 如何检测atof或wtof故障?,c++,C++,如何检测atof或wtof是否无法将字符串转换为double?但不是通过尝试检查结果是否与0.0不同,因为我的输入可以是0.0。谢谢 不要使用atof。相反,请使用中的strod,并从中选中errno: 指针e指向最后一个使用的字符上方一个。您还可以检查e==mystr,查看是否有任何字符被使用 还有std::wcstod用于处理wchar\u t-字符串,来自 在C++11中,您还有std::to_string/std::to_wstring,来自,但我认为如果转换失败,会引发异常,这可能不是

如何检测atof或wtof是否无法将字符串转换为double?但不是通过尝试检查结果是否与0.0不同,因为我的输入可以是0.0。谢谢

不要使用
atof
。相反,请使用
中的
strod
,并从
中选中
errno

指针
e
指向最后一个使用的字符上方一个。您还可以检查
e==mystr
,查看是否有任何字符被使用

还有
std::wcstod
用于处理
wchar\u t
-字符串,来自


在C++11中,您还有
std::to_string
/
std::to_wstring
,来自
,但我认为如果转换失败,会引发异常,这可能不是处理外部数据时理想的故障模式。

使用
atof
,您不能。但由于这是C++,所以建议您使用<代码> STD::StrugStuts并用<代码>操作符检查它!
在将
运算符>>
应用于
双精度

后,您刚刚发现了使用atoX函数不好的原因。@TerranceCohen:没有(但您可能仍想检查
错误号
以了解溢出和下溢。)
// assume: "char * mystr" is a null-terminated string

char * e;
errno = 0;
double x = std::strtod(mystring, &e);

if (*e != '\0' ||  // error, we didn't consume the entire string
    errno != 0 )   // error, overflow or underflow
{
    // fail
}