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

博客介绍了使用Boost库加载配置文件的方法。先在工程的包含目录和库目录中配置好Boost库,接着在.h文件导入头文件,在.cpp文件新建加载配置文件的函数,还提及了配置文件的写法,最终完成将配置文件参数加载到程序中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先在工程的包含目录和库目录中将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;
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值