一、概述

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. 不用画的动画——ShaderCp11

    --20.9.14 Shader中主要有及两种动画,一种就是纹理动画还有一种就是顶点动画. 动画效果一般都需要把时间加入一些变量的计算,以便画面可以随时间发生变化.下面是Shader中的如何去访问时间 ...

  2. Windows日常快捷键

    Windows: 环境变量: Win+R,--> sysdm.cpl 计算器: Win+R,-->calc 服务:Win+R,-->services.msc 远程:Win+R,--& ...

  3. Ubuntu linux下gcc、g++不同版本的安装和切换

    讲解linux下gcc.g++不同版本的安装和切换 Ubuntu 18.04预装GCC版本为7.3,但有时在编译是需要用的不同gcc版本,下面介绍,如何安装不同的gcc 和g++,并设置根据不同的需要 ...

  4. Vue.sync修饰符与this.$emit('update:xxx', newXXX)

    Vue通过prop进行双向数据绑定.子组件数据变化,一般只能通过 this.$emit(func, val) 回调父组件函数来传值给父组件. Vue2.3版本引入sync,作为一个事件绑定语法糖,当子 ...

  5. Spring-IoC中Set和构造器注入

    新建Maven工程 修改pom文件 1 <?xml version="1.0" encoding="UTF-8"?> 2 <project x ...

  6. Java8中Stream的用法

    Java8中Stream的用法 1.概述 Stream APl ( java.util.stream)把真正的函数式编程风格引入到Java中.这是目前为止对Java类库最好的补充,因为Stream A ...

  7. spring cloud alibaiba的POM引入

    POM添加spring cloud alibaba相关jar包 1 <dependency> 2 <groupId>org.springframework.boot</g ...

  8. ios底部安全距离

    一.使用背景 苹果官方推荐:使用env(),constant()来适配,env()和constant(),是IOS11新增特性,用于设定安全区域与边界的距离 safe-area-inset-left: ...

  9. 20181224蒋嘉豪-exp4

    20181224蒋嘉豪-exp4 目录 20181224蒋嘉豪-exp4 实验概况 1.实践目标 2.实践内容概述 知识点总结 1.有关schtasks 2.有关Sysmon(参考链接) 3.恶意代码 ...

  10. vite设置跨域

    1. vite.config.ts或者vite.config.js文件 server: { port: 3001, host: '0.0.0.0', open: true, proxy: { // 代 ...