前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++ 的 ini 配置文件读写/注释库 inicpp 用法 [ header-file-only ]

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

原创
作者头像
ThatJin
修改2024-05-14 21:51:14
2140
修改2024-05-14 21:51:14

一、库介绍

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

MIT license。

二、库使用

- git clone

代码语言:txt
复制
git clone https://github.com/dujingning/inicpp.git

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

1.读取INI文件示例

代码语言:txt
复制
#include "inicpp.hpp"
int main()
{
    // Load and parse the INI file.
    inicpp::iniReader _ini("config.ini");
    std::cout << _ini["rtsp"]["port"] << std::endl;
}

2.写示例

代码语言:txt
复制
#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.添加注释

代码语言:txt
复制
#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()

代码语言:txt
复制
#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`

代码语言:txt
复制
[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`

代码语言:txt
复制
[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`

代码语言:txt
复制
[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]$

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、库介绍
  • 二、库使用
    • - git clone
      • 1.读取INI文件示例
        • 2.写示例
          • 3.添加注释
            • * 4.toString()、toInt()、toDouble()
              • 5.完整示例:`example/main.cpp`.
                • 6.linux下使用demo的完整示例:example/main.cpp
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档