使用C++中的boost库加载配置文件

首先在工程的包含目录和库目录中将boost库配置好!
在.h文件中导入头文件

#include <boost/property_tree/ptree.hpp>  
#include <boost/property_tree/ini_parser.hpp>

在.cpp文件中新建一个加载配置文件的函数

/*----------------------------
*功能:加载配置文件参数
*-----------------------------
*输入:配置文件ini的路径
*输出:
*/
int C3D_BCSSizeDetectDlg::LoadConfig(string ConfigPath)
{
	boost::property_tree::ptree m_pt, tag_setting;
	try
	{
		read_ini(ConfigPath, m_pt);
		ShowMessage(_T("------配置文件打开成功----------"));
	}
	catch (std::exception e)
	{
		ShowMessage(_T("------配置文件打开错误,将使用默认设置----------"));
	}
	

	//Front Camera Config ini
	//Front_PointWidthRight1为宏定义参数,当配置文件加载失败时,将Front_PointWidthRight1赋值给Front_PointWidthRight

	tag_setting = m_pt.get_child("Front_Camera");        
	Front_PointWidthRight = tag_setting.get<int>("Front_PointWidthRight", Front_PointWidthRight1);
	Front_PointWidthLeft = tag_setting.get<int>("Front_PointWidthLeft", Front_PointWidthLeft1);
	Front_PointLegthFront = tag_setting.get<int>("Front_PointLegthFront", Front_PointLegthFront1);
	Front_PointLegthBehind = tag_setting.get<int>("Front_PointLegthBehind", Front_PointLegthBehind1);
	Front_PointHeightUp = tag_setting.get<int>("Front_PointHeightUp", Front_PointHeightUp1);
	Front_PointHeightDown = tag_setting.get<int>("Front_PointHeightDown", Front_PointHeightDown1);
	
	tag_setting = m_pt.get_child("Behind_Camera");
	//Behind Camera Config ini
	Behind_PointWidthRight = tag_setting.get<int>("Behind_PointWidthRight", Behind_PointWidthRight1);
	Behind_PointWidthLeft = tag_setting.get<int>("Behind_PointWidthLeft", Behind_PointWidthLeft1);
	Behind_PointLegthFront = tag_setting.get<int>("Behind_PointLegthFront", Behind_PointLegthFront1);
	Behind_PointLegthBehind = tag_setting.get<int>("Behind_PointLegthBehind", Behind_PointLegthBehind1);
	Behind_PointHeightUp = tag_setting.get<int>("Behind_PointHeightUp", Behind_PointHeightUp1);
	Behind_PointHeightDown = tag_setting.get<int>("Behind_PointHeightDown", Behind_PointHeightDown1);

	tag_setting = m_pt.get_child("Camera_To_Depth_Distance");
	DepthCameraHeight = tag_setting.get<int>("DepthCameraHeight", DepthCameraHeight1);
	
	return 1;
}

配置文件的写法
在这里插入图片描述
这样完成了加载配置文件中的参数到程序中了 !

猜你喜欢

转载自blog.csdn.net/pcb931126/article/details/89785018