简易参数外置
作者:互联网
参数外置不想弄得太麻烦,json之类移植费劲,直接就按行读取数据流,并对字符串进行解析就好了
double string2num(std::string s)
{
std::istringstream iss(s);
//iss.precision(10);///保留精度位数
double res = 0;
iss >> res;
return res;
}
int getParam()//获得外置参数
{
std::map<std::string, std::string> data;
std::string filename = "./param.txt";
std::ifstream fin(filename);
if (!fin)
{
std::cerr << "load paramter file fail...\n";
}
while (!fin.eof())
{
std::string str;
std::getline(fin, str);
if (str[0] == '#') continue;
int pos = str.find("=");
if (pos == -1)
continue;
std::string key = str.substr(0, pos);
std::string value = str.substr(pos + 1, str.length());
data[key] = value;
}
if (data.find("FX") != data.end())
{
_FX = string2num(data["FX"]);
}
else
{
std::cerr << "param :FX not found!\n";
return -1;
}
if (data.find("FY") != data.end())
{
_FY = string2num(data["FY"]);
}
else
{
std::cerr << "param :FY not found!\n";
return -1;
}
if (data.find("CX") != data.end())
{
_CX = string2num(data["CX"]);
}
else
{
std::cerr << "param :CX not found!\n";
return -1;
}
if (data.find("CY") != data.end())
{
_CY = string2num(data["CY"]);
}
else
{
std::cerr << "param :CY not found!\n";
return -1;
}
std::cout << "FX:" << _FX << ", FY:" << _FY << ", CX:" << _CX << ", CY:" << _CY <<std::endl;
return 0;
}
标签:std,iss,res,外置,filename,简易,参数,fin 来源: https://blog.csdn.net/sinat_41498648/article/details/120799831