一、概述

iniparser是针对INI文件的解析器。ini文件则是一些系统或者软件的配置文件。iniparser库的API可以对ini文件(配置文件)进行解析、设置、删除等操作。

常见的 ini 读写开源库有:minIni、inifile、iniparser

二、使用

下载

Github:https://github.com/ndevilla/iniparser

方式一

  1. 编译

    下载后进入文件根目录,使用 make 命令编译,编译完成后会生成 libiniparser.a 和 libiniparser.so.1 文件

  2. 测试

    iniparser 提供了测试程序,进入 example 目录,使用 make命令编译,完成后会生成 iniexample 执行文件

  3. 测试结果

  4. 注意事项

    使用链接文件时,可以参考 example 目录下的 Makefile 文件

方式二

此方法使用比较简单,直接将 src 目录下的文件拷贝到工程中即可,使用方式和自己编写的 .c 和 .h 文件一样

三、API函数

iniparser.h

/* 获取dictionary对象的section个数 */
int iniparser_getnsec(dictionary *d); /* 获取dictionary对象的第n个section的名字 */
char * iniparser_getsecname(dictionary *d, int n); /* 保存dictionary对象到file */
void iniparser_dump_ini(dictionary * d, FILE * f); /* 保存dictionary对象一个section到file */
void iniparser_dumpsection_ini(dictionary * d, char * s, FILE * f); /* 打印 ini 文件内容 */
void iniparser_dump(dictionary * d, FILE * f); /* 获取dictionary对象某个section下的key个数 */
int iniparser_getsecnkeys(dictionary * d, char * s); /* 获取dictionary对象某个section下所有的key */
char ** iniparser_getseckeys(dictionary * d, char * s); /* 返回dictionary对象的section:key对应的字串值 */
char * iniparser_getstring(dictionary * d, const char * key, char * def); /* 返回idictionary对象的section:key对应的整形值 */
int iniparser_getint(dictionary * d, const char * key, int notfound); /* 返回dictionary对象的section:key对应的双浮点值 */
double iniparser_getdouble(dictionary * d, const char * key, double notfound); /* 返回dictionary对象的section:key对应的布尔值 */
int iniparser_getboolean(dictionary * d, const char * key, int notfound); /* 设置dictionary对象的某个section:key的值 */
int iniparser_set(dictionary * ini, const char * entry, const char * val); /* 删除dictionary对象中某个section:key */
void iniparser_unset(dictionary * ini, const char * entry); /* 判断dictionary对象中是否存在某个section:key */
int iniparser_find_entry(dictionary * ini, const char * entry) ; /* 解析dictionary对象并返回(分配内存)dictionary对象 */
dictionary * iniparser_load(const char * ininame); /* 释放dictionary对象(内存) */
void iniparser_freedict(dictionary * d);

dictionary.h

/* 计算关键词的hash值
unsigned dictionary_hash(const char * key); /* 创建dictionary对象 */
dictionary * dictionary_new(int size); /* 删除dictionary对象 */
void dictionary_del(dictionary * vd); /* 获取dictionary对象的key值 */
char * dictionary_get(dictionary * d, const char * key, char * def); /* 设置dictionary对象的key值 */
int dictionary_set(dictionary * vd, const char * key, const char * val); /* 删除dictionary对象的key值 */
void dictionary_unset(dictionary * d, const char * key); /* 保存dictionary对象 */
void dictionary_dump(dictionary * d, FILE * out);

四、演示

  1. test.ini 文件

    #
    # 测试文件
    # [Node]
    Test = 1234
  2. test.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h> #include "iniparser.h" #define FILE_INI "test.ini" /**
    * @brief 读取 ini 文件的配置信息
    *
    * @param read_buf 读取缓冲去
    * @param return 返回操作结果
    */
    int get_ini_info(int *read_buf)
    {
    dictionary *ini; ini = iniparser_load(FILE_INI);
    if (ini==NULL) {
    fprintf(stderr, "cannot parse file: %s\n", FILE_INI);
    return -1;
    } /* 打印文件内容 */
    // iniparser_dump(ini, stderr); /* 读取压力等级的判断信息 */
    *read_buf = iniparser_getint(ini, "node:test", -1); iniparser_freedict(ini);
    return 0;
    } /**
    * @brief 写入 ini 文件的配置信息
    *
    * @param write_buf 写入缓冲区
    * @param return 返回操作结果
    */
    int set_ini_info(const char *write_buf)
    {
    dictionary *ini;
    FILE *fp = NULL; ini = iniparser_load(FILE_INI);
    if (ini==NULL) {
    fprintf(stderr, "cannot parse file: %s\n", FILE_INI);
    return -1;
    } /* 写入压力等级的判断信息 */
    iniparser_set(ini, "node:test", write_buf); /* 将信息保存到文件中 */
    fp = fopen(FILE_INI, "w");
    if( fp == NULL ) {
    fprintf(stderr, "stone:fopen error!\n");
    return -1;
    }
    iniparser_dump_ini(ini, fp); fclose(fp);
    iniparser_freedict(ini);
    return 0;
    } int main (int argc, char **argv)
    {
    int num = 0;
    set_ini_info("1234");
    get_ini_info(&num);
    printf("date is: %d \n", num);
    }
  3. 文件目录

  4. 编译

gcc test.c dictionary.c iniparser.c -o test
  1. 测试效果

参考链接

minIni:https://github.com/compuphase/minIni/tree/master/dev

inifile:https://github.com/Winnerhust/inifile2

iniparser:https://github.com/ndevilla/iniparser

Iniparser库详解:https://blog.csdn.net/weixin_46245859/article/details/125860628

Iniparser库详解:https://blog.csdn.net/weixin_46245859/article/details/125860628

C语言 ini 文件读写【Iniparser库】的更多相关文章

  1. [IO] C# INI文件读写类与源码下载 (转载)

    /// <summary> /// 类说明:INI文件读写类. /// 编 码 人:苏飞 /// 联系方式:361983679 /// 更新网站:[url]http://www.sufei ...

  2. C语言基础文件读写操作

    整理了一份C语言的文件读写件操作代码,测试时打开相应的注释即可. #include <stdio.h> #include <stdlib.h> #include <uni ...

  3. QSettings配置读写-win注册表操作-ini文件读写

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QSettings配置读写-win注册表操作-ini文件读写     本文地址:http:// ...

  4. C#对INI文件读写

    C#本身没有对INI格式文件的操作类,可以自定义一个IniFile类进行INI文件读写. using System; using System.Collections.Generic; using S ...

  5. ini文件解析c库(iniparser)

    一.交叉编译ini解析库 1.官方网站http://ndevilla.free.fr/iniparser 下载iniparser-3.1.tar.gz 2.解压 tar -zxvf iniparser ...

  6. ini文件解析c库(iniparser)【转】

    转自:http://www.cnblogs.com/dyllove98/archive/2013/07/28/3221732.html 一.交叉编译ini解析库 .官方网站http://ndevill ...

  7. C#实现.ini文件读写操作

    1.ini文件是什么?        见百度百科:https://baike.baidu.com/item/ini%E6%96%87%E4%BB%B6/9718973?fr=aladdin 2.C#语 ...

  8. 封装 INI 文件读写函数

    delphi读写ini文件实例 //--两个过程,主要实现:窗体关闭的时候,文件保存界面信息:窗体创建的时候,程序读取文件文件保存的信息. //--首先要uses IniFiles(单元) //--窗 ...

  9. VC++ 实现INI文件读写操作

    转载:https://blog.csdn.net/fan380485838/article/details/73188420 在实际项目开发中,会用ini配置文件,在此总结一下对ini读写操作 一:读 ...

  10. ini文件读写 保存上次存储内容

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

随机推荐

  1. Spring core rce 0day(CVE-2022-22965)视频教程附工具

    Spring 远程代码执行漏洞,先上图,视频晚点上传

  2. Windows MFC HTTP POST请求 函数流程

    Windows MFC HTTP POST请求 函数流程 1 CString m_strHttpUrl(_T("http://10.200.80.86:8090/course/upload& ...

  3. 我眼中的Serverless

    https://cloud.tencent.com/document/product/583 摆脱服务器.存储等底层设备,只上传代码,由云服务器提供服务的触发,维护,调用.

  4. Study python_03

    函数 基本思想---函数是用来重复使用的 def shili(input_): print("我了个去 %s"%input_) shili('你竟然') 当一个函数中即有默认参数, ...

  5. Study python_01

    Python历史事件 1989 年 --- 荷兰人吉多·范罗苏姆决心开发一个新的脚本解释程序: 1991 年 --- 第一个用C语言实现的Python编译器诞生,Python 的代码对外公布,版本为 ...

  6. win7下MongoDB安装配置

    之前看windows下安装MongoDB操作很是简单,今天在自己笔记本上安装一次,各种小问题.参照网上各大神帖子,再记录下个简单流程以便以后记得. 1.MongoDB官网上下载安装包 2.运行安装包, ...

  7. ssh通过sock5的问题kex_exchange_identification: Connection closed by remote host

    工作需要,连接几台外网的服务器.由于移动的网络直连,经常会连不上,所以想通过本地的sock5代理连接. 神奇的事情发送了,通过sock5代理,有几台主机可以连上,有几天报上面的错误. 经过反复测试,外 ...

  8. pnn模型 待整理

    https://blog.csdn.net/qq_18293213/article/details/90262378?spm=1001.2101.3001.6650.5&utm_medium= ...

  9. vue+高德地图配置及添加marker

    1.首先在index.html中引入高德地图 <script type="text/javascript" src="https://webapi.amap.com ...

  10. PLC入门笔记8

    梯形图基础电路 起保停电路 多点起保停电路 互锁控制电路 周期闪烁电路 这应该是等价的!! 定时器的接力电路 同 延时接通,延时断开电路 同 保持信号变脉冲信号电路 定时器TON 接通延时变断开延时电 ...