C++ Boost库,使用std::wstring作为Boost::property_tree::read_xml的文件名
我最近开始使用C++ Boost库,使用std::wstring作为Boost::property_tree::read_xml的文件名,c++,xml,boost,wstring,widestring,C++,Xml,Boost,Wstring,Widestring,我最近开始使用std::wstring而不是std::string,以避免使用非ASCII字符产生奇怪的结果,并且我没有找到使用boost库读取路径类型为std::wstring的XML文件的方法 我现在经常安静地使用图书馆 我将boost::property_tree::read_xml函数与boost::property_tree::wptree一起使用,而不是通常的ptree结构。但遗憾的是,我无法将std::wstring作为读取xml的第一个参数,这使得读取xml变得更加困难 我的问题
std::wstring
而不是std::string
,以避免使用非ASCII字符产生奇怪的结果,并且我没有找到使用boost库读取路径类型为std::wstring
的XML文件的方法
我现在经常安静地使用图书馆
我将boost::property_tree::read_xml
函数与boost::property_tree::wptree
一起使用,而不是通常的ptree结构。但遗憾的是,我无法将std::wstring
作为读取xml的第一个参数,这使得读取xml变得更加困难
我的问题是,在读取路径存储为std::wstring
的XML文件时,是否有解决方法
提前谢谢 我找到了一个非常简单的工作解决方案,我所做的就是使用
std::wifstream
作为boost::property_tree::read_xml
方法的第一个参数
基本上有三行代码:
boost::property_tree::wptree pt;
std::wifstream f(L"C:/äöå/file.xml");
boost::property_tree::read_xml(f, pt);
您可以使用Boost Iostreams
文件描述符\u接收器
设备,该设备支持Boost文件系统中的wpath
:
#include <boost/property_tree/xml_parser.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/filesystem.hpp>
#include <iostream>
namespace pt = boost::property_tree;
namespace io = boost::iostreams;
namespace fs = boost::filesystem;
int main()
{
fs::wpath const fname = L"test.xml";
io::file_descriptor_source fs(fname);
io::stream<io::file_descriptor_source> fsstream(fs);
pt::ptree xml;
pt::read_xml(fsstream, xml);
for (auto const& node : xml.get_child("root"))
std::cout << node.first << ": " << node.second.get_value<std::string>() << "\n";
}
这是不可移植的:-这也是为什么
boost::iostreams::file_source
(因为它依赖于std::fstream
)-正是因为这个原因,boost问题#3088被标记为“WontFix”。@sehe可以,但它在Microsoft windows上工作吗?因为我的申请还没有跨平台完成。无论如何,谢谢你提供的信息和答案。干杯。我假设它只适用于MSVC的标准库实现。(Windows上的明文、GCC、CLAN或英特尔C++编译器仍然不能工作。)这不是MSVC的第一件非标准产品。通常,wstring很少在Windows之外使用,因为只有在MSVC上,wstring才有16位字符类型,这恰好与Windows的“首选”字符编码一致UTF16@sehe我明白了,如果在windows和Visual Studio上开发,那么使用您的方法会有任何好处吗?Vcredist将安装在所有运行我的程序的计算机上。我打赌不会。如果您只希望支持MSVC扩展,您可以使用它。()如果我的文件名或路径包含非ascii字符,这是否有效?@ashishg我还没有测试过,但我认为boost::filesystem::path::codevt()
会执行相关的转换,因此:是的,我尝试修改您的示例以包含非ascii字符,但出现了错误。@ashishg适合我。任意环,注入特定的区域*
child: monkey show
child: monkey do