C++之INI配置文件读写/注释库 inicpp 介绍【简单易用-包含inicpp.hpp头文件即可】

一个头文件(header-file-only)搞定INI文件读写、甚至进行注释。跨平台,并且用法极其简单。MIT license,从此配置INI文件就像喝水。【 注:对您有帮助的话,Star或Issues为项目维护提供动力,感谢。】 - by offical of JN-inicpp project.

一、库下载

https://github.com/dujingning/inicpp 或者 https://gitee.com/dujingning/inicpp

二、库使用

1.读取INI文件示例
#include "inicpp.cpp"

int main()
{
    // Load and parse the INI file.
    inicpp::iniReader _ini("config.ini");
    std::cout << _ini["rtsp"]["port"] << std::endl;
}
2.写入INI文件示例
#include "inicpp.cpp"

int main()
{
    // Load and parse the INI file.
    inicpp::iniReader _ini("config.ini");
    _ini.modify("rtsp","port","554");
    std::cout << _ini["rtsp"]["port"] << std::endl;
}
3.注释INI文件示例
#include "inicpp.cpp"

int main()
{
    // Load and parse the INI file.
    inicpp::iniReader _ini("config.ini");
    _ini.modify("rtsp","port","554","this is the listen port for rtsp server");
    std::cout << _ini["rtsp"]["port"] << std::endl;
}
4.转换Value值toString()、toInt()、toDouble()
#include "inicpp.cpp"

int main()
{
	// Load and parse the INI file.
	inicpp::iniReader _ini("config.ini");
	_ini.modify("rtsp","port","554","this is the listen port for rtsp server");
	std::cout << _ini["rtsp"]["port"] << std::endl;

	// Convert to string, default is string
	std::string http_port_s = _ini["http"].toString("port");
	std::cout << "to string:\thttp.port = " << http_port_s << std::endl;

	// Convert to double
	double http_port_d = _ini["http"].toDouble("port");
	std::cout << "to double:\thttp.port = " << http_port_d << std::endl;

	// Convert to int
	int http_port_i = _ini["http"].toInt("port");
	std::cout << "to int:\t\thttp.port = " << http_port_i << std::endl;
}
* 5.全部功能用例见 example/main.cpp. 使用g++编译 g++ -I../ -std=c++11 main.cpp -o iniExample .
#include "inicpp.hpp"
#include <iomanip>

/* compile: g++ -I../ -std=c++11 main.cpp -o iniExample */
int main()
{

	// Load the INI file.
	inicpp::IniManager _ini("config.ini");

	// Check if the key exists.
	if (!_ini["rtsp"].isKeyExist("port"))
	{
		std::cout << "rtsp.port is not exist!" << std::endl;
	}

	// Modify or add key-value pairs.
	_ini.modify("rtsp", "port", "554");

	// Use key-value pairs directly.
	std::string rtsp_port = _ini["rtsp"]["port"];

	// You can write section-key-value.
	_ini.modify("rtsp", "port", "554");

	// Add a comment.
	_ini.modify("rtsp", "port", "554", "this is the listen port for http server");

	// Modify the comment.
	_ini.modifyComment("rtsp", "port", "this is the listen port for rtsp server ***");

	// Try to modify or add more.
	_ini.modify("math", "PI", "3.1415926", "This is pi in mathematics.");

	// You have obtained the key-value pair and saved it to your config file.
	std::cout << "to string:\trtsp.port = " << _ini["rtsp"]["port"] << std::endl;

	// Convert to string, default is string
	std::string http_port_s = _ini["math"].toString("PI");
	std::cout << "to string:\tmath.PI   = " << http_port_s << std::endl;

	// Convert to double
	double http_port_d = _ini["math"].toDouble("PI");
	std::cout << "to double:\tmath.PI   = " << std::setprecision(10) << http_port_d << std::endl;

	// Convert to int
	int http_port_i = _ini["math"].toInt("PI");
	std::cout << "to int:\t\tmath.PI   = " << http_port_i << std::endl;

	return 0;
}
  • g++编译命令过程示例
[root@jn inicpp]# ls
example  inicpp.hpp  LICENSE  README.md
[root@jn inicpp]# cd example/
[root@jn example]# grep -Rn g++
main.cpp:4:/* compile: g++ -I../ -std=c++11 main.cpp -o iniExample */
[root@jn example]# g++ -I../ -std=c++11 main.cpp -o iniExample
[root@jn example]# ls
iniExample  main.cpp
[root@jn example]# ./iniExample
rtsp.port is not exist!
to string:      rtsp.port = 554
to string:      math.PI   = 3.1415926
to double:      math.PI   = 3.1415926
to int:         math.PI   = 3
[root@jn example]# ls
config.ini  iniExample  main.cpp
[root@jn example]# cat config.ini
[rtsp]
;this is the listen port for rtsp server ***
port=554


[math]
;This is pi in mathematics.
PI=3.1415926
[root@jn example]#
posted on 2024-02-04 15:51  杜jn  阅读(62)  评论(0编辑  收藏  举报