我是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文件。
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;
}发布于 2011-09-12 23:41:58
您不需要将ini文件和hpp/cpp文件放在同一文件夹中。
dimension.ini文件与您的二进制文件(在windows上的linux .exe上可执行)位于同一文件夹中。
位置取决于您的构建系统和您的平台,而且很可能是我忘记的一些事情。
https://stackoverflow.com/questions/7389881
复制相似问题