I have an ini file containing some sample values, for example:
[Section1] Value1 = 10 Value2 = a_text_string
I am trying to download these values and print them in my application using Boost, but I don’t understand how to do this in C ++.
I searched this forum to find a few examples (I always used C, and therefore I am not very good in C ++), but I only found examples of how to read values from a file all at once.
I need to load only one value when I want, for example string = Section1.Value2 , because I do not need to read all the values, but only some of them.
I would like to load individual values and store them in a variable so I can use them whenever I want in my application.
Can this be done with Boost?
I am currently using this code:
#include <iostream> #include <string> #include <set> #include <sstream> #include <exception> #include <fstream> #include <boost/config.hpp> #include <boost/program_options/detail/config_file.hpp> #include <boost/program_options/parsers.hpp> namespace pod = boost::program_options::detail; int main() { std::ifstream s("file.ini"); if(!s) { std::cerr<<"error"<<std::endl; return 1; } std::set<std::string> options; options.insert("Test.a"); options.insert("Test.b"); options.insert("Test.c"); for (boost::program_options::detail::config_file_iterator i(s, options), e ; i != e; ++i) std::cout << i->value[0] << std::endl; }
But it just read all the values in the for loop; on the contrary, I just want to read single values when I want, and I don’t need to insert the values into the file, because it is already written with all the values that I need in my program.
c ++ boost parsing ini
Marcus Barnet May 30 '11 at 11:09 a.m. 2011-05-30 11:09
source share