一、概述

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. 22 BootStrapModelForm

    方便之处在于,我们不会再一遍一遍的写form的样式了. from django import forms class BootStrapModelForm(forms.ModelForm): def ...

  2. 树莓派4B的Node-Red编程(一)

    一.树莓派烧写 二.Node-Red 环境搭建 (一)安装Node.js (二)安装Node-Red (三)启动服务:win+R输入CMD:输入Node-red. (四)进入浏览器127.0.0.1: ...

  3. kubectl工具安装指南

    kubectl是一个用于连接Service Mesh控制平面的工具,可以安装在办公电脑的Windows系统上,也可以安装在虚拟机的Linux系统上,只要网络能与控制平面的公网地址互通即可.下面分别介绍 ...

  4. Day15-static、抽象类、接口、内部类

    static.抽象类.接口.内部类 一.static关键字详解 1.静态的变量/方法 package Demo02; //static public class Student { private s ...

  5. 【JavaScript】JS写法随笔(三) JS联动设置元素默认值

    问题: 使用DOM获取元素后setAttribute("value", "1")在页面有修改此标签value的情况下,再次触发function发生不生效.无法修 ...

  6. scala流程控制

    1.分支控制if-else 分支控制有三种:单分支.双分支.多分支: 1.1 单分支 (1).语法入下: if(条件表达式){ 执行代码块       //当条件表达式为true时,才会执行代码块内容 ...

  7. 开始 go

    为什么开始想转 go ? 毕业三年多,一直从事的是 Java , 大学学的也是 Java ,本来想一直干下去的,可是似乎 Java 水涨船高,面试要求也越来越高. 曾经一起毕业的同事自学 go ,已经 ...

  8. git 切换分支 初始化

    常见的错误 报错内容基本都是error: failed to push some refsto'远程仓库地址'. 导致产生原因 我们想把自己本地的某个项目关联到远程仓库并推送上去 操作 本地项目-&g ...

  9. C++/Lua栈操作

    一.Lua栈结构 1. index为正数 c++获取lua的数组元素的实例: // 接口参数:void lua_rawgeti (lua_State *L, int index, int n); lu ...

  10. Jmeter二、开始使用

    一.最简单的性能测试脚本 testplan→ thread group→HTTP request→view results tree.jmx后缀文件,xml文件校验 二.使用过程中其他需要注意的 1. ...