簡單小結c語言讀寫ini配置文件

參考到博客:https://www.jianshu.com/p/6088c3c2488d

簡單實用的ini文件讀取修改接口:

/*ini.h*/
#ifndef INI_H
#define INI_H

#include <stdio.h>  
#include <string.h>

int GetIniKeyString(char *title,char *key,char *filename,char *buf);

int PutIniKeyString(char *title,char *key,char *val,char *filename);

#endif /*INI_H*/
/*ini.c*/
#include <stdio.h>  
#include <string.h>        
/* 
    * 函數名:         GetIniKeyString 
    * 入口參數:        title 
    *                      配置文件中一組數據的標識 
    *                  key 
    *                      這組數據中要讀出的值的標識 
    *                  filename 
    *                      要讀取的文件路徑 
    * 返回值:         找到需要查的值則返回正確結果 0 
    *                  否則返回-1 
    */  
int GetIniKeyString(char *title,char *key,char *filename,char *buf)  
{  
    FILE *fp;  
    int  flag = 0;  
    char sTitle[64], *wTmp;
    char sLine[1024];        
    sprintf(sTitle, "[%s]", title);
                     
    if(NULL == (fp = fopen(filename, "r"))) {  
        perror("fopen");  
        return -1;
    }
    while (NULL != fgets(sLine, 1024, fp)) {  
        // 這是註釋行  
        if (0 == strncmp("//", sLine, 2)) continue;  
        if ('#' == sLine[0])              continue;        
        wTmp = strchr(sLine, '=');  
        if ((NULL != wTmp) && (1 == flag)) {  
            if (0 == strncmp(key, sLine, strlen(key))) { // 長度依文件讀取的爲準  
                sLine[strlen(sLine) - 1] = '\0';  
                fclose(fp);
                while(*(wTmp + 1) == ' '){
                    wTmp++;
                }
                strcpy(buf,wTmp + 1);
                return 0;  
            }  
        } else {  
            if (0 == strncmp(sTitle, sLine, strlen(sTitle))) { // 長度依文件讀取的爲準  
                flag = 1; // 找到標題位置  
            }  
        }  
    }  
    fclose(fp);  
    return -1;  
}        
      
/* 
    * 函數名:         PutIniKeyString 
    * 入口參數:        title 
    *                      配置文件中一組數據的標識 
    *                  key 
    *                      這組數據中要讀出的值的標識 
    *                  val 
    *                      更改後的值 
    *                  filename 
    *                      要讀取的文件路徑 
    * 返回值:         成功返回  0 
    *                  否則返回 -1 
    */  
int PutIniKeyString(char *title,char *key,char *val,char *filename)  
{  
    FILE *fpr, *fpw;  
    int  flag = 0;  
    char sLine[1024], sTitle[32], *wTmp;        
    sprintf(sTitle, "[%s]", title);  
    if (NULL == (fpr = fopen(filename, "r")))  
        return -1;// 讀取原文件  
    sprintf(sLine, "%s.tmp", filename);  
    if (NULL == (fpw = fopen(sLine,    "w")))  
        return -1;// 寫入臨時文件        
    while (NULL != fgets(sLine, 1024, fpr)) {  
        if (2 != flag) { // 如果找到要修改的那一行,則不會執行內部的操作  
            wTmp = strchr(sLine, '=');  
            if ((NULL != wTmp) && (1 == flag)) {  
                if (0 == strncmp(key, sLine, strlen(key))) { // 長度依文件讀取的爲準 
                    flag = 2;// 更改值,方便寫入文件  
                    sprintf(wTmp + 1, " %s\n", val);
                }  
            } else {
                if (0 == strncmp(sTitle, sLine, strlen(sTitle))) { // 長度依文件讀取的爲準
                    flag = 1; // 找到標題位置  
                }  
            }  
        }        
        fputs(sLine, fpw); // 寫入臨時文件 
    }  
    fclose(fpr);  
    fclose(fpw);        
    sprintf(sLine, "%s.tmp", filename);  
    return rename(sLine, filename);// 將臨時文件更新到原文件  
}    

 example1:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>  

  
void   Audio_Param_Judge()
{
     char 	l_arr_Readbuf_acapture[10]={0};
     char 	l_arr_Readbuf_aenc[10]={0};
     char 	l_arr_Readbuf_ao[10]={0};
     GetIniKeyString("acapture.0", "sample_rate", "common.ini",l_arr_Readbuf_acapture);
	 GetIniKeyString("aenc.aac", "sample_rate", "common.ini",l_arr_Readbuf_aenc);
	 GetIniKeyString("ao.0", "sample_rate", "common.ini",l_arr_Readbuf_ao);	 
	 if( (strncmp(l_arr_Readbuf_acapture,"\"48000\"",5)==0)
	  || (strncmp(l_arr_Readbuf_aenc,"\"48000\"",5) ==0 )
	  || (strncmp(l_arr_Readbuf_ao,"\"48000\"",5) == 0  ) )
	 {
	     PutIniKeyString("acapture.0", "sample_rate", "\"8000\"", "common.ini"); 
		 PutIniKeyString("aenc.aac", "sample_rate", "\"8000\"", "common.ini"); 
		 PutIniKeyString("ao.0", "sample_rate", "\"8000\"", "common.ini"); 
	 }
}



int main()
{
      Audio_Param_Judge();  
}

配置文件爲common.ini:

[ao.0]
sample_rate = "48000"

[aenc.aac]
sample_rate = "48000"

[acapture.0]
sample_rate = "48000"

執行程序後的:

[ao.0]
sample_rate = "8000"

[aenc.aac]
sample_rate = "8000"

[acapture.0]
sample_rate = "8000"

example2:

/*test.c*/
#include "ini.h"
#include <stdio.h>
int main(int argc, char const *argv[])
{
    char buff[100];
    int ret;

    ret = GetIniKeyString("city","beijing","./test.ini",buff);
    printf("ret:%d,%s\n",ret,buff);

    ret = GetIniKeyString("study","highschool","./test.ini",buff);
    printf("ret:%d,%s\n",ret,buff);

    ret = PutIniKeyString("study","highschool","zzzz","./test.ini");
    printf("put ret:%d\n",ret);
    ret = GetIniKeyString("study","highschool","./test.ini",buff);
    printf("ret:%d,%s\n",ret,buff);
    return 0;
}
ini最先內容:
/*test.ini*/
[city]
beijing =  hello-beijing
shanghai = hello-shanghai


#information
[study]
highschool = xxxx
university = yyyy






修改完成後的結果:
ret:0,hello-beijing
ret:0,xxxx
put ret:0
ret:0,zzzz

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章