user1615186
user1615186

Reputation:

How to parse more than 2 level ini file with Boost

I want to have an ini file with more than just 2 levels ... something like this

[Section1]
Value1 = 10
Value2 = a_text_string

[Section2]
[SubSection1]
Value1=1
Value2=2

[Section2]
[SubSection2]
Value1=a
Value2=b

Qn 1. How to create such ini file?

After that I want to load these values and print them in my application with Boost

*Qn2. Will this work? If not how can I do that?*

boost::property_tree::ptree pt;
boost::property_tree::ini_parser::read_ini("config.ini", pt);
std::cout << pt.get<std::string>("Section1.Value1") << std::endl;
std::cout << pt.get<std::string>("Section2.Subsection1.Value2") << std::endl;

Upvotes: 1

Views: 1699

Answers (1)

cdhowie
cdhowie

Reputation: 169133

INI files do not support structure like this. If you want to have different structural levels in an INI file, you have to specify the full path in each section:

[Section1]
Value1 = 10
Value2 = a_text_string

[Section2.SubSection1]
Value1=1
Value2=2

[Section2.SubSection2]
Value1=a
Value2=b

The actual "Section2." prefix means nothing specific in the INI grammar, it's just a way for you to create this kind of structure in a language that doesn't support it via nesting.

Upvotes: 3

Related Questions