C语言 ini 文件读写【Iniparser库】
一、概述
iniparser是针对INI文件的解析器。ini文件则是一些系统或者软件的配置文件。iniparser库的API可以对ini文件(配置文件)进行解析、设置、删除等操作。
常见的 ini 读写开源库有:minIni、inifile、iniparser
二、使用
下载
Github:https://github.com/ndevilla/iniparser
方式一
编译
下载后进入文件根目录,使用make命令编译,编译完成后会生成 libiniparser.a 和 libiniparser.so.1 文件测试
iniparser 提供了测试程序,进入 example 目录,使用make命令编译,完成后会生成 iniexample 执行文件测试结果

注意事项
使用链接文件时,可以参考 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);
四、演示
test.ini 文件
#
# 测试文件
# [Node]
Test = 1234
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);
}文件目录

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

参考链接
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库】的更多相关文章
- [IO] C# INI文件读写类与源码下载 (转载)
/// <summary> /// 类说明:INI文件读写类. /// 编 码 人:苏飞 /// 联系方式:361983679 /// 更新网站:[url]http://www.sufei ...
- C语言基础文件读写操作
整理了一份C语言的文件读写件操作代码,测试时打开相应的注释即可. #include <stdio.h> #include <stdlib.h> #include <uni ...
- QSettings配置读写-win注册表操作-ini文件读写
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QSettings配置读写-win注册表操作-ini文件读写 本文地址:http:// ...
- C#对INI文件读写
C#本身没有对INI格式文件的操作类,可以自定义一个IniFile类进行INI文件读写. using System; using System.Collections.Generic; using S ...
- ini文件解析c库(iniparser)
一.交叉编译ini解析库 1.官方网站http://ndevilla.free.fr/iniparser 下载iniparser-3.1.tar.gz 2.解压 tar -zxvf iniparser ...
- ini文件解析c库(iniparser)【转】
转自:http://www.cnblogs.com/dyllove98/archive/2013/07/28/3221732.html 一.交叉编译ini解析库 .官方网站http://ndevill ...
- C#实现.ini文件读写操作
1.ini文件是什么? 见百度百科:https://baike.baidu.com/item/ini%E6%96%87%E4%BB%B6/9718973?fr=aladdin 2.C#语 ...
- 封装 INI 文件读写函数
delphi读写ini文件实例 //--两个过程,主要实现:窗体关闭的时候,文件保存界面信息:窗体创建的时候,程序读取文件文件保存的信息. //--首先要uses IniFiles(单元) //--窗 ...
- VC++ 实现INI文件读写操作
转载:https://blog.csdn.net/fan380485838/article/details/73188420 在实际项目开发中,会用ini配置文件,在此总结一下对ini读写操作 一:读 ...
- ini文件读写 保存上次存储内容
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
随机推荐
- HCIP-ICT实战进阶04-ISIS原理与配置
HCIP-ICT实战进阶04-ISIS原理与配置 0 前言 IS-IS(Intermediate System to Intermediate System, 中间系统到中间系统)协议, 和OSPF一 ...
- C#访问MySQL(一):连接查询删除(查删)
前言: 通过C#连接访问MySQL:连接查询. 1.项目添加MySQL引用: 2.获取数据库一个满足条件的值: public static object GetSingle2(string SQLSt ...
- c++学习5 预处理
一 内存分区 内存的分区变量存储,一般可以分为以下五个区,它们分别是: 可读可写 堆区:使用malloc.calloc.realloc.free以及c++里面的new和delete去动态申请. ...
- PTA1003 我要通过! (20 分)
PTA1003 我要通过! (20 分) "答案正确"是自动判题系统给出的最令人欢喜的回复.本题属于 PAT 的"答案正确"大派送 -- 只要读入的字符串满足下 ...
- Java学习笔记2-1
2.对象容器(1) 今天学习一下Java里面的一些容器的基本功能,今天先来Arraylist. 一.Arraylist 容器类主要是为了存放一些按某些方式排列的对象,arraylist是一种容 ...
- springboot项目记录3用户注册界面
九.注册-前端页面 1.在register页面中编写发送请求的方法,采用点击事件来完成.选中对应的按钮(JQuery下的)(( " 选 择 器 " ) ) , 选 中 某 一 个 ...
- 面向对象ooDay8
精华笔记: 接口: 是一种数据类型(引用类型) 由interface定义 只能包含常量和抽象方法(所有数据默认都是常量,所有方法默认都是抽象的) 接口不能被实例化 接口是需要被实现/继承的,实现/派生 ...
- logging 模块详解
日志记录函数以它们用来跟踪的事件的级别或严重性命名.下面描述了标准级别及其适用性(从高到低的顺序) 日志等级(level) 描述DEBUG 最详细的日志信息,典型应用场景是 问题诊断INFO 信息详细 ...
- egret tween 屏幕震动动画 ts
let orig = { x: this.x, y: this.y }; var dir = 1; var tox = 0; var toy = 0; var count = 40; // if (n ...
- 狂神学习笔记domo6
1.新特性,1000000000可以写成10_0000_0000便于阅读 2.强制类型转换 先强制类型转换再赋值才能正确的结果 public class domo06 { public static ...