【发布时间】:2011-11-15 10:26:41
【问题描述】:
我是 C++ 和 boost 的新手。我正在尝试使用 boost::program_options 读取(稍后写入)INI 文件。我什至尝试使用 boost::property_tree。
当使用 std::stringstream s("[test]\n""a=2\n""b=3\n") 时,两者(program_options & property_tree) 都能完美工作,但在使用 std::ifstream 时则不行s(“维度.ini”)。我已将文件:Dimension.ini、Rcasdim.hpp/cpp 放在同一个文件夹中,并且在搜索目录中也有相关的 boost lib 文件。
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;
}
【问题讨论】:
标签: c++