开发者

boost program options parse_config_file

开发者 https://www.devze.com 2023-04-11 22:52 出处:网络
I want to save the settings (in a file) of my application in boost program_options way This is my function

I want to save the settings (in a file) of my application in boost program_options way

This is my function

void MainWindow::saveSettings()
{
    po::options_description desc("Allowed options");
    desc.add_options()
        ("ip",deImPath->text().toStdString().c_str())
        ("cp",deCalPath->text().toStdString().c_str())
        ("sp",deSolPath->text().toStdString().c_str());
    po::variables_map vm;
    po::store(po::parse_config_file("settings.conf",desc),vm);
    po::notify(vm);
}

Unfortunately I get this error:

error C2784: 'boost::program开发者_开发问答_options::basic_parsed_options<charT>
 boost::program_options::parse_config_file(std::basic_istream<charT> &,
                                           const boost::program_options::options_description &,bool)' :
 could not deduce template argument for 'std::basic_istream<charT> &' from 'const char [14]'

How can I solve this problem?


boost::program_options purpose is to pass parameters to your program.

If you want to store a configuration that the program can write too, you can use Boost.PropertyTree or Qt's QSettings class.


I had the same problem with Boost 1.49. The current documentation does not mention the

basic_parsed_options<charT>
parse_config_file(const char* filename, const options_description&,
                  bool allow_unregistered = false);

version anymore (http://www.boost.org/doc/libs/1_54_0/doc/html/boost/program_options/parse_config_f_idp87054352.html). I solved this problem with a std::ifstream like this

po::variables_map options;
po::options_description desc;

desc.add_options()
("max_processing_time",         po::value<double>()->required(),       "Maximum processing time")
("min_processing_time",         po::value<double>()->required(),       "Minimum processing time")
("relative_range_of_due_dates", po::value<double>()->required(),       "RDD Relative Range of Due Dates")
("tardiness_factor",            po::value<double>()->required(),       "TF Tardines Factor")
("number_of_jobs",              po::value<unsigned int>()->required(), "Number of jobs");

std::ifstream file(filename.c_str());
po::store(po::parse_config_file(file, desc), options);
file.close();
po::notify(options);

After that you can access the values with

max_processing_time_ = options["max_processing_time"].as<double>();
min_processing_time_ = options["min_processing_time"].as<double>();
relative_range_of_due_dates_ = options["relative_range_of_due_dates"].as<double>();
tardiness_factor_ = options["tardiness_factor"].as<double>();
number_of_jobs_ = options["number_of_jobs"].as<unsigned int>();


By my opinion it's not Boost problem - In this case it's Unicode string cast problem on windows: windows file name must be ANSI string. You need help Boost with <char> if you use Unicode charset option in Visual Studio.

void MainWindow::saveSettings()
{
    po::options_description desc("Allowed options");
    desc.add_options()
        ("ip",deImPath->text().toStdString().c_str())
        ("cp",deCalPath->text().toStdString().c_str())
        ("sp",deSolPath->text().toStdString().c_str());
    po::variables_map vm;
    po::store(po::parse_config_file<char>("settings.conf",desc),vm);
    po::notify(vm);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号