首页 > 编程语言 >关于c++使用toml plusplus(俗称toml++)的使用(2)

关于c++使用toml plusplus(俗称toml++)的使用(2)

时间:2024-08-18 23:15:38浏览次数:10  
标签:kilograms std string plusplus c++ ++ toml name

链接

toml读取

toml写入

目标

  • 目标:数组表的写入
  • 目标文件内容如下
[NET_INTERFACE]
bool = false
integer = 1234567890
string = 'this is a string'

[[fruit]]
kilograms = 2
name = 'banana'

[[fruit]]
kilograms = 1
name = 'apple'

[[fruit]]
kilograms = 3
name = 'blueberry'

关键代码

NetInterfaceConfigPropertyName propertyName;
toml::table rootNode{};
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);

/// 下一是一个数组的写入
 
{
	/// 定义了水果信息
	struct FruitInfo
	{
		/// 水果的名称
		std::string     m_name{""};
		/// 水果的重量
		int64_t         m_kilograms{0};
	};

	/// <key-水果的名称,value-水果信息>
	using HashFruitInfo = std::unordered_map<std::string, FruitInfo>;

	HashFruitInfo fruitInfoHash{{"apple", {"apple", 1}}, {"banana", {"banana", 2}}, {"blueberry", {"blueberry", 3}}};


	toml::array tmpArr;
	// for(auto& [key, value] : tmpArr)
	for (HashFruitInfo::iterator it = fruitInfoHash.begin(); it != fruitInfoHash.end(); ++ it)
	{
		toml::table tblTmp{};
		tableNodeInsert<std::string>(tblTmp, "name", it->first);
		tableNodeInsert<int64_t>(tblTmp, "kilograms", it->second.m_kilograms);

		tmpArr.insert(tmpArr.begin(), tblTmp);
	}

	/// 将数组中的内容
	netInterfaceNode.insert_or_assign("fruit", tmpArr);
}


/// 使用流打开文件
std::ofstream tomlFile(std::string{"example.toml"}, std::ios::out | std::ios::trunc);
if (tomlFile.is_open())
{
	// 使用 toml++ 的内置方法将 TOML 值写入文件
	tomlFile << netInterfaceNode;
	tomlFile.close();
}
else
{
	/// TODO
}

标签:kilograms,std,string,plusplus,c++,++,toml,name
From: https://www.cnblogs.com/pandamohist/p/18366349

相关文章

  • C++:新枚举与新结构
    一、枚举(一)C枚举?真整数!    考虑下面的程序#include<stdio.h>#include<stdlib.h>typedefenum{spring,summer,autumn,winter}Season;voidprintSeason(Seasonseason){ switch(season){ casespring: printf("spring"); break; case......
  • C++:从Type到Control
    一、基本数据类型     计算机的存储空间由最基本的二进制数(比特)组成,若干连续的二进制位(一般为8位)组成一个字节并被分配一个内存地址(),所以单独的比特没有地址,通常情况下CPU也不会一个比特一个比特读取数据,相反,字节被当作基本操作单位。在此前提下,一切要存储在计算机上的......
  • C++:函数
         FunctionsareC++entitiesthatassociateasequenceofstatements(afunctionbody)withanameandalistofzeroormorefunctionparameters.        函数是C++中的实体,它将一系列语句(一个函数体)与一个名称和零个或多个函数参数列表相关......
  • 关于c++使用toml plusplus(俗称toml++)的使用
    链接toml++-githubtoml++-帮助文档使用要求:c++17及以上版本toml语法-英文toml语法-中文toml读取参见官方给出的范例toml写入一个范例,一个开胃菜toml文件待生成的目标文件内容为[NET_INTERFACE]bool=falseinteger=1234567890string='thisis......
  • 【C++学习笔记 18】C++中的隐式构造函数
    举个例子#include<iostream>#include<string>usingString=std::string;classEntity{private: Stringm_Name; intm_Age;public: Entity(constString&name) :m_Name(name),m_Age(-1){} Entity(intage) :m_Name("UnKnown")......
  • C/C++内存管理
    一、目标1.C/C++内存分布2.C语言中动态内存管理方式3.C++中动态内存管理4.operatornew与operatordelete函数5.new和delete的实现原理6.常见面试题二、个人见解1.C/C++内存分布【说明】1.栈又叫堆栈--非静态局部变......
  • c++ 获取文件夹目录名字
        main.cpp#ifndefPHOTO_FILE_PROCESSOR_H#definePHOTO_FILE_PROCESSOR_H#include<iostream>#include<string>#include<vector>#include<dirent.h>#include<algorithm>#include<stdexcept>classPhotoFilePro......
  • C++ 设计模式——建造者模式
    建造者模式建造者模式组成部分建造者模式使用步骤1.定义产品类2.创建具体产品类3.创建建造者接口4.实现具体建造者5.创建指挥者类6.客户端代码建造者模式UML图建造者模式UML图解析建造者模式的优缺点建造者模式的适用场景完整代码建造者模式建造者模式(B......
  • typedef在C/C++的用法
    typedef是C和C++中的一个关键字,用于为已有的数据类型创建新的类型名。它的主要用途如下:1.定义别名typedef最基本的功能是为一个现有的类型定义一个别名,使代码更简洁或更具可读性。例如:typedefunsignedlongulong;ulonga,b;这段代码将unsignedlong类型重......
  • extern在头文件中添加是否必要?(C/C++)
    在C和C++编程中,extern关键字通常用于表示函数或变量的声明(而非定义),特别是在跨文件使用时。尽管在函数声明中使用extern不是强制性的,但它有特定的作用,尤其在变量声明方面。让我们深入探讨一下。1.函数声明的基本概念当你在头文件中声明一个函数时,通常只需要提供函......