简易参数外置

简易参数外置,第1张

简易参数外置

参数外置不想弄得太麻烦,json之类移植费劲,直接就按行读取数据流,并对字符串进行解析就好了

double string2num(std::string s)
{
	std::istringstream iss(s);

	//iss.precision(10);///保留精度位数

	double res = 0;
	iss >> res;
	return res;
}

int getParam()//获得外置参数
{
	std::map 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 <					
										


					

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4653741.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-06
下一篇 2022-11-06

发表评论

登录后才能评论

评论列表(0条)

保存