0
我是C++和boost中的新手。我正在嘗試使用boost :: program_options讀取(稍後寫入)INI文件。我甚至嘗試使用boost :: property_tree。無法讀取INI文件使用boost :: program_options解析
兩個(program_options & property_tree)在使用std :: stringstream s(「[test] \ n」「a = 2 \ n」「b = 3 \ n」)時是完美的,但是當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;
}
thnx for reply..why是boost :: program_options無法從我的程序中的「dimension.ini」中讀取/檢測「Key = Value」? érror在哪裏? – boosturself
'std :: ifstream s(「dimension.ini」)'這行說從當前工作目錄加載文件dimensions.ini。您需要指定路徑或在運行時確定它。它與提升無關。 – mkaes
問題在於傳遞文件名。在property_tree控制程序之前,文件名(文件)應該可以打開。簡單地說,對於這個「文件」,執行「this」(這裏是對INI文件的property_tree解析) – boosturself