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

简介: 这是一个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]$
```

目录
相关文章
|
2月前
|
API C++ Windows
Visual C++运行库、.NET Framework和DirectX运行库的作用及常见问题解决方案,涵盖MSVCP140.dll丢失、0xc000007b错误等典型故障的修复方法
本文介绍Visual C++运行库、.NET Framework和DirectX运行库的作用及常见问题解决方案,涵盖MSVCP140.dll丢失、0xc000007b错误等典型故障的修复方法,提供官方下载链接与系统修复工具使用指南。
650 2
|
2月前
|
Ubuntu API C++
C++标准库、Windows API及Ubuntu API的综合应用
总之,C++标准库、Windows API和Ubuntu API的综合应用是一项挑战性较大的任务,需要开发者具备跨平台编程的深入知识和丰富经验。通过合理的架构设计和有效的工具选择,可以在不同的操作系统平台上高效地开发和部署应用程序。
153 11
|
2月前
|
缓存 算法 程序员
C++STL底层原理:探秘标准模板库的内部机制
🌟蒋星熠Jaxonic带你深入STL底层:从容器内存管理到红黑树、哈希表,剖析迭代器、算法与分配器核心机制,揭秘C++标准库的高效设计哲学与性能优化实践。
C++STL底层原理:探秘标准模板库的内部机制
|
2月前
|
IDE 编译器 开发工具
msvcp100.dll,msvcp120.dll,msvcp140.dll,Microsoft Visual C++ 2015 Redistributable,Visual C++ 运行库安装
MSVC是Windows下C/C++开发核心工具,集成编译器、链接器与调试器,配合Visual Studio使用。其运行时库(如msvcp140.dll)为程序提供基础函数支持,常因缺失导致软件无法运行。通过安装对应版本的Microsoft Visual C++ Redistributable可解决此类问题,广泛应用于桌面软件、游戏及系统级开发。
367 2
|
3月前
|
并行计算 C++ Windows
|
存储 安全 编译器
第二问:C++中const用法详解
`const` 是 C++ 中用于定义常量的关键字,主要作用是防止值被修改。它可以修饰变量、指针、函数参数、返回值、类成员等,确保数据的不可变性。`const` 的常见用法包括:
|
11月前
|
JSON C++ 数据格式
C++20 高性能基础库--兰亭集库助力开发者构建高性能应用
这次分享的主题是《高性能基础库--兰亭集库助力开发者构建高性能应用》的实践经验。主要分为三个部分: 1. 业务背景 2. 雅兰亭库架构 3. 业务优化
368 9
|
11月前
|
XML 网络协议 API
超级好用的C++实用库之服务包装类
通过本文对Boost.Asio、gRPC和Poco三个超级好用的C++服务包装类库的详细介绍,开发者可以根据自己的需求选择合适的库来简化开发工作,提高代码的效率和可维护性。每个库都有其独特的优势和适用场景,合理使用这些库可以极大地提升C++开发的生产力。
268 11
|
存储 C++ 容器
【C++】map、set基本用法
本文介绍了C++ STL中的`map`和`set`两种关联容器。`map`用于存储键值对,每个键唯一;而`set`存储唯一元素,不包含值。两者均基于红黑树实现,支持高效的查找、插入和删除操作。文中详细列举了它们的构造方法、迭代器、容量检查、元素修改等常用接口,并简要对比了`map`与`set`的主要差异。此外,还介绍了允许重复元素的`multiset`和`multimap`。
323 3
【C++】map、set基本用法