C++ 的 ini 配置文件读写/注释库 inicpp 用法 [ header-file-only ]

本文涉及的产品
Serverless 应用引擎 SAE,800核*时 1600GiB*时
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
简介: 这是一个C++库,名为inicpp,用于读写带有注释的INI配置文件,仅包含一个hpp头文件,无需编译,支持C++11及以上版本。该库提供简单的接口,使得操作INI文件变得容易。用户可通过`git clone`从GitHub或Gitee获取库,并通过包含`inicpp.hpp`来使用`inicpp::iniReader`类。示例代码展示了读取、写入配置项以及添加注释的功能,还提供了转换为字符串、双精度和整型的函数。项目遵循MIT许可证,示例代码可在Linux环境下编译运行。

平常的ini配置文件只能读取,但是这个库不光可以读取、写入配置项,还能给配置项写注释。只有一个hpp头文件,不需要编译,支持C++11及之后版本。

MIT license,配置INI文件就像喝水一样简单。

一、库下载

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

二、库使用

- git clone

git clone https://github.com/dujingning/inicpp.git

包含 inicpp.hpp,声明类 inicpp::iniReader,然后就可以随意使用了.

1.读取INI文件示例

#include "inicpp.hpp"

int main()
{
   
    // Load and parse the INI file.
    inicpp::iniReader _ini("config.ini");
    std::cout << _ini["rtsp"]["port"] << std::endl;
}

2.写示例

#include "inicpp.hpp"

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.添加注释

#include "inicpp.hpp"

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.toString()、toInt()、toDouble()

#include "inicpp.hpp"

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.

编译demo到example目录下make一下就好了: example/Makefile .

没有make命令,只需要执行: g++ -I../ -std=c++11 main.cpp -o iniExample

6.linux下使用demo的完整示例:example/main.cpp

  • 编译 example/main.cpp

    [jn@jn inicpp]$ ls
    example  inicpp.hpp  LICENSE  README.md
    [jn@jn inicpp]$ cd example/
    [jn@jn example]$ make
    g++ -I../ -std=c++11 main.cpp -o iniExample
    [jn@jn example]$ ls
    iniExample  main.cpp  Makefile
    
  • 运行 iniExample

    [jn@jn example]$ ./iniExample
    get rtsp port:555
    to string:      rtsp.port = 554
    to string:      math.PI   = 3.1415926
    to string:      math.PI   = 3.1415926
    to double:      math.PI   = 3.1415926
    to int:         math.PI   = 3
    to wstring:     other.desc= 你好,世界
    [jn@jn example]$
    
  • 生成 config.ini
    ```bash
    [jn@jn example]$ cat config.ini
    ;no section test:add comment later.
    noSection=yes
    key0=noSectionAndComment
    key1=noSectionAndComment
    key2=noSectionAndComment
    [head]
    ;thanks for your using inicpp project.
    title=inicpp
    ;Permissive license for open-source software distribution.
    license=MIT

[rtsp]
;this is the listen ip for rtsp server.
port=554
ip=127.0.0.1

[math]
;This is pi in mathematics.
PI=3.1415926

[other]
;this test for std::wstring. comment it.
desc=你好,世界
[jn@jn example]$
```

目录
相关文章
|
18天前
|
存储 自然语言处理 安全
C++ STL标准库 《string原理与实战分析》
C++ STL标准库 《string原理与实战分析》
20 0
|
9天前
|
存储 算法 C++
C++一分钟之-标准模板库(STL)简介
【6月更文挑战第21天】C++ STL是高效通用的算法和数据结构集,简化编程任务。核心包括容器(如vector、list)、迭代器、算法(如sort、find)和适配器。常见问题涉及内存泄漏、迭代器失效、效率和算法误用。通过示例展示了如何排序、遍历和查找元素。掌握STL能提升效率,学习过程需注意常见陷阱。
22 4
|
3天前
|
域名解析 网络协议 程序员
程序员必知:【转】adns解析库——域名解析实例(C++、linux)
程序员必知:【转】adns解析库——域名解析实例(C++、linux)
11 0
|
3天前
|
C++ 容器
C++ STL标准库 《map容器详解》
C++ STL标准库 《map容器详解》
9 0
|
4天前
|
存储 C++ 容器
C++ STL标准库 《map容器详解》
C++ STL标准库 《map容器详解》
11 0
|
4天前
|
缓存 C++
详细解读C++常用库函数C函数库cstdio
详细解读C++常用库函数C函数库cstdio
|
4天前
|
域名解析 网络协议 程序员
程序员必知:【转】adns解析库——域名解析实例(C++、linux)
程序员必知:【转】adns解析库——域名解析实例(C++、linux)
|
17天前
|
大数据 C++ 索引
C++ STL标准库 《vector向量原理与实战分析》
C++ STL标准库 《vector向量原理与实战分析》
19 0
|
18天前
|
C++ 容器
C++ STL标准库 《queue单向队列原理与实战分析》
C++ STL标准库 《queue单向队列原理与实战分析》
18 0
|
23天前
|
Java API Android开发
Java通过JNI调用C++的DLL库
Java通过JNI调用C++的DLL库
12 0