一个头文件(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文件示例
Copy Highlighter-hljs code-theme-dark
#include "inicpp.cpp"
int main ()
{
inicpp::iniReader _ini("config.ini" );
std::cout << _ini["rtsp" ]["port" ] << std::endl;
}
2.写入INI文件示例
Copy Highlighter-hljs code-theme-dark
#include "inicpp.cpp"
int main ()
{
inicpp::iniReader _ini("config.ini" );
_ini.modify ("rtsp" ,"port" ,"554" );
std::cout << _ini["rtsp" ]["port" ] << std::endl;
}
3.注释INI文件示例
Copy Highlighter-hljs code-theme-dark
#include "inicpp.cpp"
int main ()
{
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()
Copy Highlighter-hljs code-theme-dark
#include "inicpp.cpp"
int main ()
{
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;
std::string http_port_s = _ini["http" ].toString ("port" );
std::cout << "to string:\thttp.port = " << http_port_s << std::endl;
double http_port_d = _ini["http" ].toDouble ("port" );
std::cout << "to double:\thttp.port = " << http_port_d << std::endl;
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
.
Copy Highlighter-hljs code-theme-dark
#include "inicpp.hpp"
#include <iomanip>
int main ()
{
inicpp::IniManager _ini("config.ini" );
if (!_ini["rtsp" ].isKeyExist ("port" ))
{
std::cout << "rtsp.port is not exist!" << std::endl;
}
_ini.modify ("rtsp" , "port" , "554" );
std::string rtsp_port = _ini["rtsp" ]["port" ];
_ini.modify ("rtsp" , "port" , "554" );
_ini.modify ("rtsp" , "port" , "554" , "this is the listen port for http server" );
_ini.modifyComment ("rtsp" , "port" , "this is the listen port for rtsp server ***" );
_ini.modify ("math" , "PI" , "3.1415926" , "This is pi in mathematics." );
std::cout << "to string:\trtsp.port = " << _ini["rtsp" ]["port" ] << std::endl;
std::string http_port_s = _ini["math" ].toString ("PI" );
std::cout << "to string:\tmath.PI = " << http_port_s << std::endl;
double http_port_d = _ini["math" ].toDouble ("PI" );
std::cout << "to double:\tmath.PI = " << std::setprecision (10 ) << http_port_d << std::endl;
int http_port_i = _ini["math" ].toInt ("PI" );
std::cout << "to int:\t\tmath.PI = " << http_port_i << std::endl;
return 0 ;
}
Copy Highlighter-hljs code-theme-dark
[root@jn inicpp]
example inicpp.hpp LICENSE README.md
[root@jn inicpp]
[root@jn example]
main.cpp:4:/* compile: g++ -I../ -std=c++11 main.cpp -o iniExample */
[root@jn example]
[root@jn example]
iniExample main.cpp
[root@jn example]
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]
config.ini iniExample main.cpp
[root@jn example]
[rtsp]
;this is the listen port for rtsp server ***
port=554
[math]
;This is pi in mathematics.
PI=3.1415926
[root@jn example]
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人