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
本文来自博客园,作者:浇筑菜鸟,转载请注明原文链接:https://www.cnblogs.com/jzcn/p/17236030.html
如本博客的内容侵犯了你的权益,请与以下地址联系,本人获知后,马上删除。同时本人深表歉意,并致以崇高的谢意! cn_jiaozhu@qq.com
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通