首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用boost::program_options读取INI文件解析

无法使用boost::program_options读取INI文件解析
EN

Stack Overflow用户
提问于 2011-09-12 22:59:59
回答 1查看 752关注 0票数 0

我是c++和boost的新手。我正在尝试使用boost::program_options读取(稍后写入) INI文件。我甚至尝试过使用boost::property_tree。

当使用std::stringstream s(“program_options\n”“a=2\n”“b=3\n”)时,两者(test和property_tree)都能很好地工作,但当使用std::ifstream s("dimension.ini")时就不行了。我已经将文件: dimension.ini,Rcasdim.hpp/cpp放在同一个文件夹中,在搜索目录中也有相关的boost lib文件。

代码语言:javascript
复制
INI File

[Section]

a=2

b=3

Purpose:  
I need to dynamically set the "Value" (at the start ONLY) for a Particular "Key" in INI file & Later USE that Previously set "Value" for that "Key" by other project files (more, as a toggle)

 #include boost/program_options/detail/config_file.hpp

 #include boost/program_options/parsers.hpp


namespace pod = boost::program_options::detail;

class CRcasdim
{
    public:
    CRcasdim(){};
    ~CRcasdim(){};
    std::string getrcasdim(float);

    private:
    std::string sd;
};

std::string CRcasdim::getrcasdim(float d)

{

    //std::stringstream s("[Section]\n""a=2\n""b=3\n"); WORKS

    std::ifstream s("dimension.ini"); DOESNT WORK

    if(!s)
    {
        std::cerr<<"error"<<std::endl;
    }

    std::set<std::string> options;
    std::map<std::string, std::string> parameters;

    options.insert("Section.a");
    options.insert("Section.b");

try

{

    for (pod::config_file_iterator i(s, options), e ; i != e; ++i)

        parameters[i->string_key] = i->value[0];   
}

catch(std::exception& e)

{
std::cerr<<"Exception: ";
}

    if (d==2)
    sd = parameters["Section.a"];

    else if (d==3)
    sd = parameters["Section.b"];

    return sd;
}
EN

回答 1

Stack Overflow用户

发布于 2011-09-12 23:41:58

您不需要将ini文件和hpp/cpp文件放在同一文件夹中。

dimension.ini文件与您的二进制文件(在windows上的linux .exe上可执行)位于同一文件夹中。

位置取决于您的构建系统和您的平台,而且很可能是我忘记的一些事情。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7389881

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档