gpt4 book ai didi

c++ - 使用 wcstod 转换零值

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:06:07 26 4
gpt4 key购买 nike

我正在使用 wcstod() 将我从 XML 文件中读取的数字类型文本数据转换为:

double x;
BSTR name;
MSXML::IXMLDOMNodeListPtr theList;
MSXML::IXMLDOMNodePtr theItem;

//Some XML API here...

theItem = theList->Getitem(0);
theItem->get_text(&name);
x = wcstod(name,NULL);

问题是这个函数在失败时返回 NULL,但有时我确实想读取并转换有效的字符串 L"0"。有解决方法吗?

最佳答案

您可以使用 std::stod如果函数失败,它将抛出异常。

确保 BSTR 被初始化为 NULL。为 Getitemget_text

添加错误检查
#include <string>

BSTR name = nullptr;
...
double x = 0;
if(name)
{
try
{
x = std::stod(name);
}
catch(...)
{
//error ...
}
}

关于c++ - 使用 wcstod 转换零值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50036996/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com