Boost读写ini文件
文章目录
写在前面
INI
文件是一种非常简单的文件格式,通常以 “.ini” 作为文件扩展名,用于存储应用程序的设置和配置信息。INI
文件由一个或多个节(section)组成,每个节都以一个用方括号括起来的标题开头,包含一个或多个键值对(key=value),用于存储具体的配置信息。INI
文件为应用程序提供了一种简单、易于编辑的方式来存储和管理配置信息。
- 测试环境:
Win11
+Vs2015
+Boost1.80
- 这里默认读者已经安装好
Boost
库,如何不知道如何安装可查看Boost编译使用_boost 编译 使用
准备工作
-
需要用到的头文件
#include <boost/property_tree/ini_parser.hpp> #include <boost/property_tree/ptree.hpp>
写文件
- 下面是个简单的写ini文件的例子
void ini_write(std::string &fileName) {
boost::property_tree::ptree pt;
pt.put<std::string>("Student.Name", "XiaoMing");
pt.put<int>("Student.Age", 15);
pt.put<std::string>("Student.Sex", "man");
pt.put<int>("Student.Class", 1);
boost::property_tree::ini_parser::write_ini(fileName, pt);
}
- ini文件中的内容
[Student]
Name=XiaoMing
Age=15
Sex=man
Class=1
读文件
键值查找
- 这里读取刚才写的ini文件,通过键直接读取。
void ini_read_key(std::string &fileName) {
boost::property_tree::ptree pt;
boost::property_tree::ini_parser::read_ini(fileName, pt);
std::string st_name = pt.get<std::string>("Student.Name");
int st_age = pt.get<int>("Student.Age");
std::string st_sex = pt.get<std::string>("Student.Sex");
int st_class = pt.get<int>("Student.Class");
std::cout << "Name: " << st_name << std::endl;
std::cout << "Age: " << st_age << std::endl;
std::cout << "Sex: " << st_sex << std::endl;
std::cout << "Class: " << st_class << std::endl;
}
/* output:
Name: XiaoMing
Age: 15
Sex: man
Class: 1
*/
遍历
- 依然是读取刚才的ini文件,通过遍历的方式。
void ini_read(std::string &fileName) {
boost::property_tree::ptree pt;
boost::property_tree::ini_parser::read_ini(fileName, pt);
for (const auto §ion : pt) { // 遍历根节点下的所有子节点
std::cout << "Section: " << section.first << std::endl;
for (const auto& pair : section.second) { // 遍历当前节点下的所有键值对
std::cout << " Key: " << pair.first << ", Value: " << pair.second.get_value<std::string>() << std::endl;
}
}
}
/* output:
Section: Student
Key: Name, Value: XiaoMing
Key: Age, Value: 15
Key: Sex, Value: man
Key: Class, Value: 1
*/
- 遍历指定节点
void ini_read(std::string &fileName) {
// 遍历指定节点
boost::property_tree::ptree& section = pt.get_child("Student");
for (const auto& pair : section) {
std::cout << " Key: " << pair.first << ", Value: " << pair.second.get_value<std::string>() << std::endl;
}
}
/* output:
Key: Name, Value: XiaoMing
Key: Age, Value: 15
Key: Sex, Value: man
Key: Class, Value: 1
*/
设置默认值
- 当我们通过键值查找时,如何文件中并没有我们查找的键值,可以通过设置默认值来避免异常(引发程序崩溃)。
- 默认值可再使用
get
键后下一个参数写入默认值,下面的Height
参数。
void ini_read_key(std::string &fileName) {
boost::property_tree::ptree pt;
boost::property_tree::ini_parser::read_ini(fileName, pt);
std::string st_name = pt.get<std::string>("Student.Name");
int st_age = pt.get<int>("Student.Age");
std::string st_sex = pt.get<std::string>("Student.Sex");
int st_class = pt.get<int>("Student.Class");
int st_height = pt.get<int>("Height",175); // 错误键值,无默认值时触发异常
std::cout << "Name: " << st_name << std::endl;
std::cout << "Age: " << st_age << std::endl;
std::cout << "Sex: " << st_sex << std::endl;
std::cout << "Class: " << st_class << std::endl;
std::cout << "Height: " << st_height << std::endl;
}
/* output:
Name: XiaoMing
Age: 15
Sex: man
Class: 1
Height: 175
*/
异常处理
- 这里
Height
没有设置默认值,使程序产生一个异常,并用try
void ini_read_key(std::string &fileName) {
try {
boost::property_tree::ptree pt;
boost::property_tree::ini_parser::read_ini(fileName, pt);
std::string st_name = pt.get<std::string>("Student.Name");
int st_age = pt.get<int>("Student.Age");
std::string st_sex = pt.get<std::string>("Student.Sex");
int st_class = pt.get<int>("Student.Class");
int st_height = pt.get<int>("Height"); // 错误键值,无默认值时触发异常
std::cout << "Name: " << st_name << std::endl;
std::cout << "Age: " << st_age << std::endl;
std::cout << "Sex: " << st_sex << std::endl;
std::cout << "Class: " << st_class << std::endl;
std::cout << "Height: " << st_height << std::endl;
}
catch (boost::property_tree::ini_parser_error& e)
{
std::cerr << e.what() << "\n";
}
catch (std::exception& e)
{
std::cerr << e.what() << "\n";
}
}
/* output:
No such node (Height)
*/