我想与您分享我在尝试使用Boost库(版本1.52.0)处理C++中XML元素的某些属性时遇到的问题.给出以下代码:
#define ATTR_SET ".<xmlattr>"
#define XML_PATH1 "./pets.xml"
#include <iostream>
#include <string>
#include <boost/foreach.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
using namespace std;
using namespace boost;
using namespace boost::property_tree;
const ptree& empty_ptree(){
static ptree t;
return t;
}
int main() {
ptree tree;
read_xml(XML_PATH1, tree);
const ptree & formats = tree.get_child("pets", empty_ptree());
BOOST_FOREACH(const ptree::value_type & f, formats){
string at = f.first + ATTR_SET;
const ptree & attributes = formats.get_child(at, empty_ptree());
cout << "Extracting attributes from " << at << ":" << endl;
BOOST_FOREACH(const …Run Code Online (Sandbox Code Playgroud) 我想知道如何使用 C++ 读取/写入 JSON 文件。我将使用此文件来存储我正在制作的简单游戏的玩家信息和设置。这没什么花哨的,只是一个控制台猜数字游戏,但我只是用它来学习东西。
我必须知道如何读取和写入 JSON 的特定部分。