关于c++使用toml plusplus(俗称toml++)的使用
链接#
-
使用要求: c++ 17 及以上版本
toml 完整版读取与写入#
toml读取#
toml写入#
- 一个范例,一个开胃菜
toml文件#
- 待生成的目标文件内容为
[NET_INTERFACE]
bool = false
integer = 1234567890
string = 'this is a string'
关键代码#
toml.h
头文件的代码
/// ----------------------------------------------------
/// @file toml.h
/// @author oct (oct@oct.com)
/// @brief toml中常用的一些函数的转换
/// @version 0.1
/// @date 2024-08-12
///
/// @copyright Copyright (c) 2024
///
/// ----------------------------------------------------
#ifndef TOML_H_
#define TOML_H_
#include <string>
#include <toml.hpp>
namespace oct
{
/// @brief 插入非string节点
/// @param whichTable -哪个表
/// @param key - 插入的数据值
/// @param value -值
template<typename T>
static void tableNodeInsert(toml::table& whichTable, const std::string& key, const T& value)
{
whichTable.insert_or_assign(key, toml::value<T>(value));
}
}
#endif ///! TOML_H_
写文件关键代码#
/// 定义了表的一些键的名称
NetInterfaceConfigPropertyName propertyName;
toml::table rootNode{};
/// 参数1-哪个表,参数2-节点的key, 参数3-将要插入的值
tableNodeInsert<std::string>(rootNode, "string", "this is a string");
tableNodeInsert<bool>(rootNode, "bool", false);
tableNodeInsert<int64_t>(rootNode, "integer", 1234567890);
toml::table netInterfaceNode{};
/// 创建[NET_INTERFACE]
netInterfaceNode.insert_or_assign(propertyName.m_tableName, rootNode);
/// 使用流打开文件
std::globalToml{"EXAMPLE.toml"};
std::ofstream tomlFile(globalToml, std::ios::out | std::ios::trunc);
if (tomlFile.is_open())
{
// 使用 toml++ 的内置方法将 TOML 值写入文件
tomlFile << netInterfaceNode;
tomlFile.close();
}
else
{
std::cout << "failed to write file";
return 2;
}
作者: mohist
出处:https://www.cnblogs.com/pandamohist/p/18366194
版权:本站使用「CC BY 4.0」创作共享协议,未经作者同意,请勿转载;若经同意转载,请在文章明显位置注明作者和出处。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2021-08-18 cmake之if
2021-08-18 cmake之譬判断cmake的版本