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 ...
随机推荐
- ngnix开启高可用网关集群--好记性不如烂笔头
#1.开启高可用网关集群--------------------好记性不如烂笔头--------------------------- #gzip on; #开启网关集群 upstream gatew ...
- 利用matlab求解函数微分
利用matlab解决求解函数微分 matlab,微分 1. 问题提出 最近在复习高等数学,感觉可以结合 去理解他. 遇到了一个题目: 2. 具体代码 %{ 解决函数微分问题 %} clc; clear ...
- 【BOOK】正则表达式
正则表达式 1. 开源中国-正则表达式测试工具:https://tool.oschina.net/regex/ 2. 匹配规则 3. match() 从字符串起始位置匹配正则表达式 若从起始位置匹配不 ...
- 1.2 C语言--函数与数组
函数 函数的定义 返回值类型函数名(类型形参名[,--]){ 函数体 } 除了没有访问修饰符外,基本等同于java的函数. 良好的程序设计风格要求即使没有返回值,也要使用return;作为最后一条语句 ...
- 使用php为本地html文件生成url
1. 查看你是不是有php which php 2. 假设你的Demo.html 路径为 /Users/Sheron/Downloads/Demo.html cd /Users/Sheron/Dow ...
- sonar使用
代码质量检查工具 sonar 1. 下载,版本sonar 4.5.1 运行bin下的bat文件,浏览器中访问: http://localhost:9000 , 成功. 2. 修改数据库为mysql数据 ...
- mysql零基础-2
更新中的数据完整性错误 UPDATE employees SET department_id = 55 WHERE department_id = 110; 删除数据 删除一条记录 DELETE FR ...
- UI自动化之【maven+selenium环境搭建】
一.下载maven包 官网: http://maven.apache.org/download.cgi 二.配置maven环境变量 配置完之后验证一下:(若出现以下信息可看到maven的版本号就表示 ...
- sqlite3 replace函数服务器端替换一个字段中数据的例子 ;拼接字段字符串
1.把字段filePath中所有类似 '/usr/local/Trolltech/%'的字符串都替换成 '/zzzzz/' update EstDlpFileAttribute set ...
- jquery的ajax方法获取不到return返回值
/** 2 * 方式:(1)同步调用 (2)在ajax函数中return值 3 * 结果:返回 1.未成功获取返回值 4 * 失败原因:ajax内部是一个或多个定义的函数,ajax中return返回值 ...