1
"A": "1"
"A.B": "2"
"A.C": "3"

How to get the value of A.B if i iterate through the ptree it works. if i try to get value of pt.get_child("A\.B").get_value<std::string>(). i get the following exception

terminate called after throwing an instance of boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::property_tree::ptree_bad_path> >'
      what():  No such node

please find the complete code below

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <iostream>
#include <string>
#include <iterator>

using boost::property_tree::ptree;

/* Indent Json Output */
std::string indent(int level) {
    std::string s;
    for (int i = 0; i < level; i++) s += "  ";
    return s;
}

/* Print tree in json format */
void printTree(ptree & pt, int level) {
    if (pt.empty()) {
        std::cerr << "\"" << pt.data() << "\"";
    } else {
        if (level) std::cerr << std::endl;
        std::cerr << indent(level) << "{" << std::endl;
        for (ptree::iterator pos = pt.begin(); pos != pt.end();) {
            std::cerr << indent(level + 1) << "\"" << pos-> first << "\": ";

            printTree(pos->second, level + 1);
            ++pos;
            if (pos != pt.end()) {
                std::cerr << ",";
            }
            std::cerr << std::endl;
        }
        std::cerr << indent(level) << " }";
    }
    return;
}

int main()
{
ptree pt;
read_ini("sample.ini", pt);
printTree(pt, 0);
std::cout << pt.get_child("A.B").get_value<std::string>() << std::endl; //tries to resolve A.B to two nodes    
std::cout << pt.get_child("A\\.B").get_value<std::string>() << std::endl; //error

}

sample.ini

A=1
A.B=2
A.C=3
raghu
  • 95
  • 1
  • 10
  • 1
    Hi can anyone please tell me why the downvote, what needs to be changed. Is any info required!? – raghu May 11 '18 at 08:20
  • I completely agree. This is a fine question for anyone who knows the library involved. (I'm not sure whey your text says `ini.get_child`, but the sample doesn't appear to be INI format.) – sehe May 11 '18 at 09:09
  • i have added a proper example. – raghu May 11 '18 at 09:18

1 Answers1

1

You can use alternative path delimiters, but it's a bit tricky and not very well documented.

You have to temporarily specify an alternative path separator:

Live On Coliru

#include <boost/property_tree/ini_parser.hpp>
#include <iostream>
using boost::property_tree::ptree;

int main() {
    ptree pt;

    pt.put("a.b", "first");
    pt.put(ptree::path_type("a|complicated.name", '|'), "second");

    write_ini(std::cout, pt);
}

Prints

[a]
b=first
complicated.name=second
sehe
  • 374,641
  • 47
  • 450
  • 633
  • thanks for the response. but I am reading from the sample.ini. sorry for misunderstanding. – raghu May 11 '18 at 09:18
  • 1
    That is not relevant to the answer. Just use `path_type` with an explicit delimiter (any character that you don't otherwise use). [Note I answered before adding the comment. The comment was just in response to people downvoting the question.] – sehe May 11 '18 at 09:20
  • 2
    `pt.get_child(ptree::path_type("A.B", '|').get_value()` thanks this works – raghu May 11 '18 at 09:24