C語(yǔ)言Iniparser庫(kù)實(shí)現(xiàn)ini文件讀寫
一、概述
iniparser是針對(duì)INI文件的解析器。ini文件則是一些系統(tǒng)或者軟件的配置文件。iniparser庫(kù)的API可以對(duì)ini文件(配置文件)進(jìn)行解析、設(shè)置、刪除等操作。
常見(jiàn)的 ini 讀寫開(kāi)源庫(kù)有:minIni、inifile、iniparser
二、使用
下載
Github:https://github.com/ndevilla/iniparser
方式一
1.編譯
下載后進(jìn)入文件根目錄,使用 make 命令編譯,編譯完成后會(huì)生成 libiniparser.a 和 libiniparser.so.1 文件
2.測(cè)試
iniparser 提供了測(cè)試程序,進(jìn)入 example 目錄,使用 make命令編譯,完成后會(huì)生成 iniexample 執(zhí)行文件
3.測(cè)試結(jié)果
4.注意事項(xiàng)
使用鏈接文件時(shí),可以參考 example 目錄下的 Makefile 文件
方式二
此方法使用比較簡(jiǎn)單,直接將 src 目錄下的文件拷貝到工程中即可,使用方式和自己編寫的 .c 和 .h 文件一樣
三、API函數(shù)
iniparser.h
/* 獲取dictionary對(duì)象的section個(gè)數(shù) */ int iniparser_getnsec(dictionary *d); /* 獲取dictionary對(duì)象的第n個(gè)section的名字 */ char * iniparser_getsecname(dictionary *d, int n); /* 保存dictionary對(duì)象到file */ void iniparser_dump_ini(dictionary * d, FILE * f); /* 保存dictionary對(duì)象一個(gè)section到file */ void iniparser_dumpsection_ini(dictionary * d, char * s, FILE * f); /* 打印 ini 文件內(nèi)容 */ void iniparser_dump(dictionary * d, FILE * f); /* 獲取dictionary對(duì)象某個(gè)section下的key個(gè)數(shù) */ int iniparser_getsecnkeys(dictionary * d, char * s); /* 獲取dictionary對(duì)象某個(gè)section下所有的key */ char ** iniparser_getseckeys(dictionary * d, char * s); /* 返回dictionary對(duì)象的section:key對(duì)應(yīng)的字串值 */ char * iniparser_getstring(dictionary * d, const char * key, char * def); /* 返回idictionary對(duì)象的section:key對(duì)應(yīng)的整形值 */ int iniparser_getint(dictionary * d, const char * key, int notfound); /* 返回dictionary對(duì)象的section:key對(duì)應(yīng)的雙浮點(diǎn)值 */ double iniparser_getdouble(dictionary * d, const char * key, double notfound); /* 返回dictionary對(duì)象的section:key對(duì)應(yīng)的布爾值 */ int iniparser_getboolean(dictionary * d, const char * key, int notfound); /* 設(shè)置dictionary對(duì)象的某個(gè)section:key的值 */ int iniparser_set(dictionary * ini, const char * entry, const char * val); /* 刪除dictionary對(duì)象中某個(gè)section:key */ void iniparser_unset(dictionary * ini, const char * entry); /* 判斷dictionary對(duì)象中是否存在某個(gè)section:key */ int iniparser_find_entry(dictionary * ini, const char * entry) ; /* 解析dictionary對(duì)象并返回(分配內(nèi)存)dictionary對(duì)象 */ dictionary * iniparser_load(const char * ininame); /* 釋放dictionary對(duì)象(內(nèi)存) */ void iniparser_freedict(dictionary * d);
dictionary.h
/* 計(jì)算關(guān)鍵詞的hash值 unsigned dictionary_hash(const char * key); /* 創(chuàng)建dictionary對(duì)象 */ dictionary * dictionary_new(int size); /* 刪除dictionary對(duì)象 */ void dictionary_del(dictionary * vd); /* 獲取dictionary對(duì)象的key值 */ char * dictionary_get(dictionary * d, const char * key, char * def); /* 設(shè)置dictionary對(duì)象的key值 */ int dictionary_set(dictionary * vd, const char * key, const char * val); /* 刪除dictionary對(duì)象的key值 */ void dictionary_unset(dictionary * d, const char * key); /* 保存dictionary對(duì)象 */ void dictionary_dump(dictionary * d, FILE * out);
四、演示
1.test.ini 文件
#
# 測(cè)試文件
#
[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 返回操作結(jié)果 */ 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; } /* 打印文件內(nèi)容 */ // iniparser_dump(ini, stderr); /* 讀取壓力等級(jí)的判斷信息 */ *read_buf = iniparser_getint(ini, "node:test", -1); iniparser_freedict(ini); return 0; } /** * @brief 寫入 ini 文件的配置信息 * * @param write_buf 寫入緩沖區(qū) * @param return 返回操作結(jié)果 */ 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; } /* 寫入壓力等級(jí)的判斷信息 */ 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
5.測(cè)試效果
到此這篇關(guān)于C語(yǔ)言Iniparser庫(kù)實(shí)現(xiàn)ini文件讀寫的文章就介紹到這了,更多相關(guān)C語(yǔ)言 ini文件讀寫內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++?JSON庫(kù)?nlohmann::basic_json::array?的用法示例詳解
nlohmann::json是一個(gè)C++的JSON庫(kù),它提供了一種容易和直觀的方法來(lái)處理JSON數(shù)據(jù),nlohmann::json::array()是用來(lái)創(chuàng)建一個(gè)JSON數(shù)組的方法,這篇文章主要介紹了C++ JSON庫(kù)nlohmann::basic_json::array的用法,需要的朋友可以參考下2023-06-06C++驅(qū)動(dòng)bash的實(shí)現(xiàn)代碼
這篇文章主要介紹了C++驅(qū)動(dòng)bash的實(shí)現(xiàn)代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-11-11C++基本用法實(shí)踐之移動(dòng)語(yǔ)義詳解
移動(dòng)(move)語(yǔ)義是C++引入了一種新的內(nèi)存優(yōu)化,以避免不必要的拷貝,下面小編就來(lái)和大家簡(jiǎn)單聊聊C++中移動(dòng)語(yǔ)義的相關(guān)使用吧,希望對(duì)大家有所幫助2023-07-07C++中template方法undefined reference to的問(wèn)題解決
Undefined reference to 錯(cuò)誤:這類錯(cuò)誤是在連接過(guò)程中出現(xiàn)的,本文就來(lái)介紹一下C++中template方法undefined reference to的問(wèn)題解決,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03C++實(shí)現(xiàn)LeetCode(56.合并區(qū)間)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(56.合并區(qū)間),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07Qt入門學(xué)習(xí)之?dāng)?shù)據(jù)庫(kù)操作指南
Qt SQL模塊為數(shù)據(jù)庫(kù)提供了編程支持,Qt支持很多種常見(jiàn)的數(shù)據(jù)庫(kù),如 MySQL Oracle、MS SQL Server、SQLite等,下面這篇文章主要介紹了這篇文章主要給大家介紹了關(guān)于Qt入門學(xué)習(xí)之?dāng)?shù)據(jù)庫(kù)操作指南的相關(guān)資料,需要的朋友可以參考下2022-08-08