相关疑难解决方法(0)

如何使用Boost解析ini文件

我有一个ini文件,其中包含一些示例值,如:

[Section1]
Value1 = 10
Value2 = a_text_string
Run Code Online (Sandbox Code Playgroud)

我正在尝试加载这些值并使用Boost在我的应用程序中打印它们,但我不明白如何在C++中执行此操作.

我在这个论坛中搜索,以便找到一些例子(我总是使用C,所以我在C++中不是很好)但我只找到了关于如何一次性从文件中读取值的示例.

我需要在我想要的时候只加载一个值,比如string = Section1.Value2因为我不需要读取所有的值,而只需要读取其中的一些值.

我想加载单个值并将它们存储在变量中,以便在我的应用程序中使用它们时使用它们.

使用Boost可以做到这一点吗?

目前,我正在使用此代码:

#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;
   }
Run Code Online (Sandbox Code Playgroud)

但这只是读取for循环中的所有值; 相反,我只想在需要时读取单个值,并且我不需要在文件中插入值,因为它已经用我在程序中需要的所有值写入.

c++ ini parsing boost

60
推荐指数
1
解决办法
6万
查看次数

如何从R脚本中的配置文件中获取参数

有没有办法从R脚本中的文件中读取参数?

我想创建一个配置文件

db_host=xxxx
db_name=xxxx
db_user=xxxx
db_pass=xxxx
Run Code Online (Sandbox Code Playgroud)

然后在R脚本中使用它来创建数据库连接.

dbConnect(PgSQL(), host="xxxx", dbname="xxxxx", user="xxxx", password="xxxxx")
Run Code Online (Sandbox Code Playgroud)

然后我如何在R脚本中使用它.

编辑:我也想知道是否有一种方法可以在R Scripts,Perl Scripts和Java中使用单个配置文件?

unix r

30
推荐指数
2
解决办法
1万
查看次数

标签 统计

boost ×1

c++ ×1

ini ×1

parsing ×1

r ×1

unix ×1