c++ - 如何实现文件读取和创建到结构中
问题描述
不知道如何实现读取数据并放入结构体
有一个 config.txt 文件,它存储数据,如
someBigText=ASDSDdasdsa (can be more +1000 simbol)
isOkay=true
myAge=24
struct Config {
std::string Name;
std::string StringValue;
};
解决方案
假设您的配置分为以下部分:
[myImportantSection]
someBigText = "foo tha can be more +1000 simbols"
isOkay = true
myAge = 24
并假设您正在使用 boost:
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
boost::property_tree::ptree myPtree;
boost::property_tree::ini_parser::read_ini("config.txt", myPtree);
auto text{myPtree.get<std::string>("myImportantSection.someBigText")};
auto isOk{myPtree.get<bool>("myImportantSection.isOkay")};
auto age{myPtree.get<int>("myImportantSection.myAge")};
struct Config
{
std::string text{};
bool ok{false};
int age{0};
};
Config myConfig;
myConfig.text = text;
myConfig.ok = isOk;
myConfig.age = age;
推荐阅读
- spring - 无法连接到运行 Junit 测试的 Redis
- arrays - 在另一个数组 SWIFT 4 中循环遍历数组和分组连续数字的最佳方法?
- r - 安装失败:似乎不是 R 包(没有说明
- .net - 使用 DataReceivedHandler 打开一个 VB 窗体
- laravel - Laravel Nova:使用 Passport 进行身份验证
- firebase - Firestore 规则路径不起作用
- xamarin - Cosmos DB - 无法从 Xamarin 中检索
- javascript - 办公室未定义
- c# - 使用(LINQ to XML)从 XML 填充 C# 动态 TreeView
- python - 比较两个元组列表字典以计算 F 分数