欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C語言Iniparser庫實(shí)現(xiàn)ini文件讀寫

 更新時間:2023年03月20日 14:50:21   作者:澆筑菜鳥  
iniparser是針對INI文件的解析器。ini文件則是一些系統(tǒng)或者軟件的配置文件。本文就來介紹一下如何利用Iniparser庫實(shí)現(xiàn)ini文件讀寫吧

一、概述

iniparser是針對INI文件的解析器。ini文件則是一些系統(tǒng)或者軟件的配置文件。iniparser庫的API可以對ini文件(配置文件)進(jìn)行解析、設(shè)置、刪除等操作。

常見的 ini 讀寫開源庫有:minIni、inifile、iniparser

二、使用

下載

Github:https://github.com/ndevilla/iniparser

方式一

1.編譯

下載后進(jìn)入文件根目錄,使用 make 命令編譯,編譯完成后會生成 libiniparser.a 和 libiniparser.so.1 文件

2.測試

iniparser 提供了測試程序,進(jìn)入 example 目錄,使用 make命令編譯,完成后會生成 iniexample 執(zhí)行文件

3.測試結(jié)果

4.注意事項(xiàng)

使用鏈接文件時,可以參考 example 目錄下的 Makefile 文件

方式二

此方法使用比較簡單,直接將 src 目錄下的文件拷貝到工程中即可,使用方式和自己編寫的 .c 和 .h 文件一樣

三、API函數(shù)

iniparser.h

/* 獲取dictionary對象的section個數(shù) */
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 文件內(nèi)容 */
void iniparser_dump(dictionary * d, FILE * f);  
 
/* 獲取dictionary對象某個section下的key個數(shù) */
int iniparser_getsecnkeys(dictionary * d, char * s); 
 
/* 獲取dictionary對象某個section下所有的key */
char ** iniparser_getseckeys(dictionary * d, char * s); 
 
/* 返回dictionary對象的section:key對應(yīng)的字串值 */
char * iniparser_getstring(dictionary * d, const char * key, char * def);   
 
/* 返回idictionary對象的section:key對應(yīng)的整形值 */
int iniparser_getint(dictionary * d, const char * key, int notfound); 
 
/* 返回dictionary對象的section:key對應(yīng)的雙浮點(diǎn)值 */
double iniparser_getdouble(dictionary * d, const char * key, double notfound);  
 
/* 返回dictionary對象的section:key對應(yīng)的布爾值 */
int iniparser_getboolean(dictionary * d, const char * key, int notfound); 
 
/* 設(shè)置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對象并返回(分配內(nèi)存)dictionary對象 */
dictionary * iniparser_load(const char * ininame); 
 
/* 釋放dictionary對象(內(nèi)存) */
void iniparser_freedict(dictionary * d);

dictionary.h

/* 計(jì)算關(guān)鍵詞的hash值  
unsigned dictionary_hash(const char * key); 
 
/* 創(chuàng)建dictionary對象 */
dictionary * dictionary_new(int size);  
 
/* 刪除dictionary對象 */
void dictionary_del(dictionary * vd); 
 
/* 獲取dictionary對象的key值 */
char * dictionary_get(dictionary * d, const char * key, char * def); 
 
/* 設(shè)置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 返回操作結(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);
 
    /* 讀取壓力等級的判斷信息 */
    *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;
    }
 
    /* 寫入壓力等級的判斷信息 */
    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.測試效果

到此這篇關(guān)于C語言Iniparser庫實(shí)現(xiàn)ini文件讀寫的文章就介紹到這了,更多相關(guān)C語言 ini文件讀寫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C++?JSON庫?nlohmann::basic_json::array?的用法示例詳解

    C++?JSON庫?nlohmann::basic_json::array?的用法示例詳解

    nlohmann::json是一個C++的JSON庫,它提供了一種容易和直觀的方法來處理JSON數(shù)據(jù),nlohmann::json::array()是用來創(chuàng)建一個JSON數(shù)組的方法,這篇文章主要介紹了C++ JSON庫nlohmann::basic_json::array的用法,需要的朋友可以參考下
    2023-06-06
  • C語言打印楊輝三角示例匯總

    C語言打印楊輝三角示例匯總

    本文給大家匯總了2種使用C語言實(shí)現(xiàn)楊輝三角的方法,不過還是給大家推薦第一種,希望大家能夠喜歡。
    2016-02-02
  • C++驅(qū)動bash的實(shí)現(xiàn)代碼

    C++驅(qū)動bash的實(shí)現(xiàn)代碼

    這篇文章主要介紹了C++驅(qū)動bash的實(shí)現(xiàn)代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-11-11
  • Arduino控制舵機(jī)詳解 附代碼

    Arduino控制舵機(jī)詳解 附代碼

    rduino是一款便捷靈活、方便上手的開源電子原型平臺,它構(gòu)建于開放原始碼simple I/O介面版,并且具有使用類似Java、C語言的Processing/Wiring開發(fā)環(huán)境,這篇文章主要介紹了Arduino控制舵機(jī)詳解(含代碼),需要的朋友可以參考下
    2023-05-05
  • C++基本用法實(shí)踐之移動語義詳解

    C++基本用法實(shí)踐之移動語義詳解

    移動(move)語義是C++引入了一種新的內(nèi)存優(yōu)化,以避免不必要的拷貝,下面小編就來和大家簡單聊聊C++中移動語義的相關(guān)使用吧,希望對大家有所幫助
    2023-07-07
  • C++中類型推斷(auto和decltype)的使用

    C++中類型推斷(auto和decltype)的使用

    在C++11之前,每個數(shù)據(jù)類型都需要在編譯時顯示聲明,在運(yùn)行時限制表達(dá)式的值,但在C++的新版本之后,引入了 auto 和 decltype等關(guān)鍵字,本文就來介紹一下C++中類型推斷(auto和decltype)的使用,感興趣的可以了解一下
    2023-12-12
  • 判斷一個無向圖是否為連通圖的方法

    判斷一個無向圖是否為連通圖的方法

    今天小編就為大家分享一篇關(guān)于判斷一個無向圖是否為連通圖的方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • C++中template方法undefined reference to的問題解決

    C++中template方法undefined reference to的問題解決

    Undefined reference to 錯誤:這類錯誤是在連接過程中出現(xiàn)的,本文就來介紹一下C++中template方法undefined reference to的問題解決,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-03-03
  • C++實(shí)現(xiàn)LeetCode(56.合并區(qū)間)

    C++實(shí)現(xiàn)LeetCode(56.合并區(qū)間)

    這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(56.合并區(qū)間),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • Qt入門學(xué)習(xí)之?dāng)?shù)據(jù)庫操作指南

    Qt入門學(xué)習(xí)之?dāng)?shù)據(jù)庫操作指南

    Qt SQL模塊為數(shù)據(jù)庫提供了編程支持,Qt支持很多種常見的數(shù)據(jù)庫,如 MySQL Oracle、MS SQL Server、SQLite等,下面這篇文章主要介紹了這篇文章主要給大家介紹了關(guān)于Qt入門學(xué)習(xí)之?dāng)?shù)據(jù)庫操作指南的相關(guān)資料,需要的朋友可以參考下
    2022-08-08

最新評論