3

私は最近、ASCII 以外の文字を使用した奇妙な結果を避けるためstd::wstringに代わりに使用し始めましたが、boost ライブラリを使用したstd::stringタイプのパスである XML ファイルを読み取る方法が見つかりませんでした。std::wstring

私は最近、boostライブラリの quiet をよく使用しています。

通常の ptree 構造体の代わりにboost::property_tree::read_xmlwith 関数を使用します。boost::property_tree::wptreeしかし、悲しいことにstd::wstring、最初のパラメーターとして a を read_xml に渡すことができないため、すべてが難しくなります。

私の質問は、パスが として格納されている XML ファイルを読み取るための回避策はありstd::wstringますか?

前もって感謝します!

4

2 に答える 2

4

Boost Filesystem からサポートする Boost Iostreamsfile_descriptor_sinkデバイスを使用できます。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";
}

入力ファイルを使用するColiruでのライブを参照してください。

<root>
    <child nodetype="element" with="attributes">monkey show</child>
    <child nodetype="element">monkey do</child>
</root>

と印刷:

child: monkey show
child: monkey do
于 2014-08-29T21:59:13.013 に答える
0

std::wifstreamメソッドの最初のパラメーターとしてを使用するだけでしたboost::property_tree::read_xml

基本的に 3 行のコード:

boost::property_tree::wptree pt;
std::wifstream f(L"C:/äöå/file.xml");
boost::property_tree::read_xml(f, pt);
于 2014-08-29T21:58:50.483 に答える