gpt4 book ai didi

c++ - Boost property_tree错误:在.ini文件中获取元素时,数据转换为 “j”类型失败

转载 作者:行者123 更新时间:2023-12-02 09:53:49 39 4
gpt4 key购买 nike

我有一个.ini文件,其中包含以下数据

[SYSTEM]
num_of_vps = 1
而且我有这段代码可以读取.ini文件中的元素。 ( uint定义为 typedef unsigned int uint)
boost::property_tree::ptree pt; 
boost::property_tree::ini_parser::read_ini(iniFilePath, pt);
hwCount = pt.get<uint>("SYSTEM.num_of_vps");
我从包含以上代码的文件创建了一个文件,并在 main.cc文件的包装函数中将其调用。然后我得到以下错误

terminate called after throwing an instance of 'boost_1_68_0::exception_detail::clone_impl<boost_1_68_0::exception_detail::error_info_injector<boost_1_68_0::property_tree::ptree_bad_data> >'what(): conversion of data to type "j" failedstack trace

#12 0x00002aaab613fcd5 in abort () from /lib64/libc.so.6
#13 0x00002aaab9b29315 in __gnu_cxx::__verbose_terminate_handler () at ../../../../src/gcc-7.3.0/libstdc++-v3/libsupc++/vterminate.cc:95
#14 0x00002aaab9a9e8f6 in __cxxabiv1::__terminate (handler=<optimized out>) at ../../../../src/gcc-7.3.0/libstdc++-v3/libsupc++/eh_terminate.cc:47
#15 0x00002aaab9a9e941 in std::terminate () at ../../../../src/gcc-7.3.0/libstdc++-v3/libsupc++/eh_terminate.cc:57
#16 0x00002aaab9a9ea74 in __cxxabiv1::__cxa_throw (obj=<optimized out>, tinfo=0x2aaab9e1ff60 <typeinfo for boost_1_68_0::exception_detail::clone_impl<boost_1_68_0::exception_detail::error_info_injector<boost_1_68_0::property_tree::ptree_bad_data> >>, dest=0x2aaab99bef18 <boost_1_68_0::exception_detail::clone_impl<boost_1_68_0::exception_detail::error_info_injector<boost_1_68_0::property_tree::ptree_bad_data> >::~clone_impl()>) at ../../../../src/gcc-7.3.0/libstdc++-v3/libsupc++/eh_throw.cc:93
#17 0x00002aaab99bec82 in boost_1_68_0::throw_exception<boost_1_68_0::exception_detail::error_info_injector<boost_1_68_0::property_tree::ptree_bad_data> > (e=...) at throw_exception.hpp:72
#18 0x00002aaab99be576 in boost_1_68_0::exception_detail::throw_exception_<boost_1_68_0::property_tree::ptree_bad_data> (x=..., current_function=0x2aaab9b45fc0 <boost_1_68_0::enable_if<boost_1_68_0::property_tree::detail::is_translator<boost_1_68_0::property_tree::stream_translator<char, std::char_traits<char>, std::allocator<char>, unsigned int> >, unsigned int>::type boost_1_68_0::property_tree::basic_ptree<std::string, std::string, std::less<std::string> >::get_value<unsigned int, boost_1_68_0::property_tree::stream_translator<char, std::char_traits<char>, std::allocator<char>, unsigned int> >(boost_1_68_0::property_tree::stream_translator<char, std::char_traits<char>, std::allocator<char>, unsigned int>) const::__PRETTY_FUNCTION__> "typename boost_1_68_0::enable_if<boost_1_68_0::property_tree::detail::is_translator<Translator>, Type>::type boost_1_68_0::property_tree::basic_ptree<Key, Data, KeyCompare>::get_value(T"..., file=0x2aaab9b45830 "property_tree/detail/ptree_implementation.hpp", line=675) at throw_exception.hpp:89
#19 0x00002aaab99be01e in boost_1_68_0::property_tree::basic_ptree<std::string, std::string, std::less<std::string> >::get_value<unsigned int, boost_1_68_0::property_tree::stream_translator<char, std::char_traits<char>, std::allocator<char>, unsigned int> > (this=0xbc86c8, tr=...) at property_tree/detail/ptree_implementation.hpp:673
#20 0x00002aaab99bd6a5 in boost_1_68_0::property_tree::basic_ptree<std::string, std::string, std::less<std::string> >::get_value<unsigned int> (this=0xbc86c8) at property_tree/detail/ptree_implementation.hpp:683
#21 0x00002aaab99bc545 in boost_1_68_0::property_tree::basic_ptree<std::string, std::string, std::less<std::string> >::get<unsigned int> (this=0x7fffffff9470, path=...) at property_tree/detail/ptree_implementation.hpp:754
#22 0x00002aaab99bba83 in MyRT::DUTConfigFile::readIniFile (this=0xbc5d50, iniFilePath=...) at DUTConfigFile.cpp:231
#23 0x00002aaab99ba8d2 in MyRT::DUTConfigFile::DUTConfigFile (this=0xbc5d50, iniFilePath=..., configFilePath=...) at DUTConfigFile.cpp:26
#24 0x00002aaab99c0839 in setupMyConfigs () at SimXLInterface.cpp:83
#31 0x0000000000408847 in main ()
我尝试了gdb,当使用istringstream将字符串转换为uint时,它引发了异常。以下是两种过渡类型
template<class K, class D, class C>                                                                                                                    
template<class Type> inline
Type basic_ptree<K, D, C>::get_value() const
{
return get_value<Type>(
typename translator_between<data_type, Type>::type());
}

(gdb) p typeid(Type).name()
$2 = 0x2aaab5c33c91 <typeinfo name for unsigned int> "j"
(gdb) p typeid(data_type).name()
could not find typeinfo symbol for 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >'
basic_ios.h中的以下函数引发 __throw_bad_cast()异常(此处为 this->_M_num_get = NULL)
const __num_get_type& __ng = __check_facet(this->_M_num_get);
内部构建自动化Makefile默认将 -D_GLIBCXX_USE_CXX11_ABI=0添加到目标文件创建中
g++ -I/usr/myboost/boost/boost -Wall -std=c++14 -fopenmp -m64 -msse2 -fPIC \
-D_GLIBCXX_USE_CXX11_ABI=0 -g -o *.o -fPIC -c *.cpp
.so建筑命令
g++ -m64 -msse2 -pthread -shared -static-libstdc++ -static-libgcc  \
-Wl,-znoexecstack -g -o runtime.so *.o -ldl -lrt -lz -fopenmp -lcrypto
如果我将其更改为读取std::string类型,然后将其转换为uint,则它将正常工作
std::string strHwCount = pt.get<std::string>("SYSTEM.num_of_vps");
hwCount = static_cast<uint>(std::stoul(strHwCount));
我正在使用 boost-1.68gcc-7.3。这与我使用的 ABI宏有关吗?没有以上解决方法,有没有办法解决这个问题

更新
首先,我将项目从cmake迁移到内部自定义构建自动化(Makefile的某些增强版本)。在cmake中,这很好。根据sehe的回答,我做了一些进一步的分析。
在一个小的独立示例中,此错误无法重现(使用本地字符串和实际的.ini文件读取)。
通过gdb时,在 boost/property_tree/stream_translator.hpp文件中,我可以在 num_of_vps = 4string中找到从文件读取的值(已在文件中更改为 istringstream)。
optional<Type> basic_ptree<K, D, C>
::get_value_optional(Translator tr) const
{
return tr.get_value(data());
}

(gdb) p data()
$1 = "4"

boost_1_68_0::optional<E> get_value(const internal_type &v) {
std::basic_istringstream<Ch, Traits, Alloc> iss(v);
iss.imbue(m_loc);
E e;
customized::extract(iss, e);
....
}

(gdb) p v
$1 = "4"
(gdb) p iss.str()
$2 = "4"
在上面的 customized::extract(iss, e)函数内部,有一个使用 stringunsigned intbasic_istream转换
static void extract(std::basic_istream<Ch, Traits>& s, E& e) {
s >> e;
if(!s.eof()) { s >> std::ws; }
}
在此 >>运算符内有一个 _M_extract函数,在该 __check_facet函数内引发异常(通过检查 this->_M_num_get的NULL)
basic_istream<_CharT, _Traits>::
_M_extract(_ValueT& __v) {
__try {
const __num_get_type& __ng = __check_facet(this->_M_num_get);
...

inline const _Facet&
__check_facet(const _Facet* __f) {
if (!__f)
__throw_bad_cast();
return *__f;
}
方案上方的堆栈跟踪
#0  std::istream::_M_extract<unsigned int>(unsigned int&) (__f=0x0) at gcc-7.3.0/objdir/x86_64-centos-linux/libstdc++-v3/include/bits/basic_ios.h:49
#1 std::istream::operator>>(unsigned int&) (this=0x7fffffff9980, __n=@0x7fffffff997c: 0)
#2 boost_1_68_0::property_tree::customize_stream::extract(std::istream&, unsigned int&) (s=..., e=@0x7fffffff997c: 0)
#3 boost_1_68_0::property_tree::stream_translator<char, std::char_traits<char>, std::allocator<char>, unsigned int>::get_value(std::string const&) (this=0x7fffffff9b58, v="4")
在另一个我们使用 std::stringstream 的地方,我可以看到相同的行为。即使 ss运算符添加了字符串,变量 <<仍显示为空
std::stringstream ss;
ss << std::setw(8) << std::setfill('0') << std::hex << firmId;
我担心 ABI标志会导致此问题,但是我能够将其删除,问题仍然存在。我搜索了这种 istream问题,但找不到任何有用的东西。

最佳答案

您的输入已损坏。可能它使用了您没有想到的编码或代码点,但对人类读者并不突出。

Live On Coliru

#include <boost/property_tree/ini_parser.hpp>
#include <iostream>

int main() {
std::istringstream iss(R"([SYSTEM]
num_of_vps = 1)");
boost::property_tree::ptree pt;
read_ini(iss, pt);

uint hwCount = pt.get<uint>("SYSTEM.num_of_vps");

std::cout << "hwCount: " << hwCount << "\n";
}

版画
hwCount: 1

但是,如果您将输入更改为例如
    std::istringstream iss(R"([SYSTEM]
num_of_vps = 1)");

(请注意 =后面的空格现在是 < > 160, \240, U+00A0 NO-BREAK SPACE, ^KNS, &nbsp;)它将打印:

Live On Coliru

版画
terminate called after throwing an instance of 'boost::wrapexcept<boost::property_tree::ptree_bad_data>'
what(): conversion of data to type "j" failed

关于c++ - Boost property_tree错误:在.ini文件中获取元素时,数据转换为 “j”类型失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62058547/

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