I am trying to use filenames as the key in boost::PropertyTree
However, the '.' character in a filename such as "example.txt" causes an additional layer to be added within the property tree. The most obvious solution would be to replace '.' with another character, but there is likely a better way to do this, such as with an escape character.
In the following example, the value 10 will be put in the node 'txt', a child of 'example'. Instead, I want the value 10 to be stored in the node 'example.txt'.
ptree pt;
pt.put("example.txt", 10);
How can I use the full filename for a single node?
Thanks in advance for your help!
Just insert the tree explicitly:
pt.push_back(ptree::value_type("example.txt", ptree(10)));
The put
method is simply there for convenience, which is why it automatically parses .
as an additional layer. Constructing the value_type explicitly like I have shown above avoids this problem.
An alternative way to solve the problem is to use an extra argument in put
and get
, which changes the delimeter.
pt.put('/', "example.txt", "10");
pt.get<string>('/', "example.txt");
For the record, I've never used this class before in my life. I got all this information right from the page you linked to ; )
The problem was that the documentation was outdated. A path type object must be created as follows, with another character that is invalid for file paths specified as the delimiter as follows:
pt.put(boost::property_tree::ptree::path_type("example.txt", '|'), 10);
I found a path to the solution from the boost mailing list at the newsgroup gmane.comp.lib.boost.devel posted by Philippe Vaucher.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With