Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6110889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:34:03+00:00 2026-05-23T14:34:03+00:00

I have some trouble reading from an ini file using boost program options. The

  • 0

I have some trouble reading from an ini file using boost program options. The problem is a key which contains hashmarks (simplyfied example):

[section]
key=”xxx#yyy”

Retrieving the key, returns “xxx”, which is because the hashmark seems to be interpreted as start of a comment and therefore the rest of the line is skipped. Unfortunately I cannot substitute the ‘#’ by some other character because the value is a regular expression. I didn’t find a way to quote the hashmark and would prefer not to do it, because it would alter my regex and make it even more unreadable.

Is there a way to deal with this problem without rewriting the ini-file parser?
Thanks for any help.

My code to retrieve the key looks like:

std::string key;
boost::program_options::options_description opDesc("test");
opDesc.add_options()("section.key", po::value<string>(&key))
std::ifstream ifs("file.ini");
boost::program_options::parse_config_file(ifs, opDesc);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:34:03+00:00Added an answer on May 23, 2026 at 2:34 pm

    Perhaps it is time to start using Boost Property Tree as you are way past the ‘I’m parsing program options’ point here, really.

    http://www.boost.org/doc/libs/1_46_1/doc/html/property_tree.html

    Property tree has parsers/formatters for JSON, Xml, Ini (<-- you are here) and the INFO formats. The linked page specifies in a few lines exactly what round-trips (most things roundtrip, except for JSON sepcific type information and sometimes trailing whitespace).

    I suppose you’d like the INI format (because it’s close to what you’re using) and the INFO setting (because it has more string syntax, in addition to hierarchically nested sections).


    From the samples:

    #include <boost/property_tree/ptree.hpp>
    #include <boost/property_tree/xml_parser.hpp>
    #include <boost/foreach.hpp>
    #include <string>
    #include <set>
    #include <exception>
    #include <iostream>
    
    struct debug_settings
    {
        std::string m_file;               // log filename
        int m_level;                      // debug level
        std::set<std::string> m_modules;  // modules where logging is enabled
        void load(const std::string &filename);
        void save(const std::string &filename);
    };
    
    void debug_settings::load(const std::string &filename)
    {
        using boost::property_tree::ptree;
        ptree pt;
        read_xml(filename, pt);
        m_file = pt.get<std::string>("debug.filename");
        m_level = pt.get("debug.level", 0);
        BOOST_FOREACH(ptree::value_type &v, pt.get_child("debug.modules"))
            m_modules.insert(v.second.data());
    }
    
    void debug_settings::save(const std::string &filename)
    {
        using boost::property_tree::ptree;
        ptree pt;
        pt.put("debug.filename", m_file);
        pt.put("debug.level", m_level);
        BOOST_FOREACH(const std::string &name, m_modules)
            pt.add("debug.modules.module", name);
        write_xml(filename, pt);
    }
    
    int main()
    {
        try
        {
            debug_settings ds;
            ds.load("debug_settings.xml");
            ds.save("debug_settings_out.xml");
            std::cout << "Success\n";
        }
        catch (std::exception &e)
        {
            std::cout << "Error: " << e.what() << "\n";
        }
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble reading in supplementary unicode characters using Java. I have a file
I have some trouble achieving adequate real-time performance from my application and wondering if
I'm have some trouble with the fulltext CONTAINS operator. Here's a quick script to
I'm trying to remove vowels from a text file and am having some trouble.
I have a problem with some excel code that I am having trouble getting
I have a problem with a c++ socket. I'm using CAsyncSocket from MFC that
I have written a program which reads a file containing multidimensional data (most commonly
I have some trouble with my trac installation (version 11.4). What should I do
I have some trouble with jquery ui-events: In my application, there are some sliders.
I am mlearning javascript and have some trouble creating an onject via prototype. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.