C 讀取ini文件的實(shí)例詳解
C 讀取ini文件
前言:
在Windows下可以用GetPrivateProfileString或GetPrivateProfileInt方便讀取.ini配置文件內(nèi)容,但是在Linux平臺(tái)上就一籌莫展了。為了解決該問(wèn)題,打算用C來(lái)讀取.ini,即可不受平臺(tái)的限制了。
#define CONF_FILE_PATH "Config.ini" #include <string.h> #ifdef WIN32 #include <Windows.h> #include <stdio.h> #else #define MAX_PATH 260 #include <unistd.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <stdarg.h> #endif char g_szConfigPath[MAX_PATH]; //獲取當(dāng)前程序目錄 int GetCurrentPath(char buf[],char *pFileName) { #ifdef WIN32 GetModuleFileName(NULL,buf,MAX_PATH); #else char pidfile[64]; int bytes; int fd; sprintf(pidfile, "/proc/%d/cmdline", getpid()); fd = open(pidfile, O_RDONLY, 0); bytes = read(fd, buf, 256); close(fd); buf[MAX_PATH] = '\0'; #endif char * p = &buf[strlen(buf)]; do { *p = '\0'; p--; #ifdef WIN32 } while('\\' != *p ); #else } while('/' != *p ); #endif p++; //配置文件目錄 memcpy(p,pFileName,strlen(pFileName)); return 0; } //從INI文件讀取字符串類型數(shù)據(jù) char *GetIniKeyString(char *title,char *key,char *filename) { FILE *fp; char szLine[1024]; static char tmpstr[1024]; int rtnval; int i = 0; int flag = 0; char *tmp; if((fp = fopen(filename,"r")) == NULL) { printf("have no such file \n"); return ""; } while(!feof(fp)) { rtnval = fgetc(fp); if(rtnval == EOF) { break; } else { szLine[i++] = rtnval; } if(rtnval == '\n') { #ifndef WIN32 i--; #endif szLine[--i] = '\0'; i = 0; tmp = strchr(szLine, '='); if(( tmp != NULL )&&(flag == 1)) { if(strstr(szLine,key)!=NULL) { //注釋行 if ('#' == szLine[0]) { } else if ( '\/' == szLine[0] && '\/' == szLine[1] ) { } else { //找打key對(duì)應(yīng)變量 strcpy(tmpstr,tmp+1); fclose(fp); return tmpstr; } } } else { strcpy(tmpstr,"["); strcat(tmpstr,title); strcat(tmpstr,"]"); if( strncmp(tmpstr,szLine,strlen(tmpstr)) == 0 ) { //找到title flag = 1; } } } } fclose(fp); return ""; } //從INI文件讀取整類型數(shù)據(jù) int GetIniKeyInt(char *title,char *key,char *filename) { return atoi(GetIniKeyString(title,key,filename)); } int main(int argc,char* argv[]) { char buf[MAX_PATH]; memset(buf,0,sizeof(buf)); GetCurrentPath(buf,CONF_FILE_PATH); strcpy(g_szConfigPath,buf); int iCatAge; char szCatName[32]; iCatAge = GetIniKeyInt("CAT","age",g_szConfigPath); strcpy(szCatName,GetIniKeyString("CAT","name",g_szConfigPath)); return 0; }
#define CONF_FILE_PATH "Config.ini" #include <string.h> #ifdef WIN32 #include <Windows.h> #include <stdio.h> #else #define MAX_PATH 260 #include <unistd.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <stdarg.h> #endif char g_szConfigPath[MAX_PATH]; //獲取當(dāng)前程序目錄 int GetCurrentPath(char buf[],char *pFileName) { #ifdef WIN32 GetModuleFileName(NULL,buf,MAX_PATH); #else char pidfile[64]; int bytes; int fd; sprintf(pidfile, "/proc/%d/cmdline", getpid()); fd = open(pidfile, O_RDONLY, 0); bytes = read(fd, buf, 256); close(fd); buf[MAX_PATH] = '\0'; #endif char * p = &buf[strlen(buf)]; do { *p = '\0'; p--; #ifdef WIN32 } while( '\\' != *p ); #else } while( '/' != *p ); #endif p++; //配置文件目錄 memcpy(p,pFileName,strlen(pFileName)); return 0; } //從INI文件讀取字符串類型數(shù)據(jù) char *GetIniKeyString(char *title,char *key,char *filename) { FILE *fp; char szLine[1024]; static char tmpstr[1024]; int rtnval; int i = 0; int flag = 0; char *tmp; if((fp = fopen(filename, "r")) == NULL) { printf("have no such file \n"); return ""; } while(!feof(fp)) { rtnval = fgetc(fp); if(rtnval == EOF) { break; } else { szLine[i++] = rtnval; } if(rtnval == '\n') { #ifndef WIN32 i--; #endif szLine[--i] = '\0'; i = 0; tmp = strchr(szLine, '='); if(( tmp != NULL )&&(flag == 1)) { if(strstr(szLine,key)!=NULL) { //注釋行 if ('#' == szLine[0]) { } else if ( '\/' == szLine[0] && '\/' == szLine[1] ) { } else { //找打key對(duì)應(yīng)變量 strcpy(tmpstr,tmp+1); fclose(fp); return tmpstr; } } } else { strcpy(tmpstr,"["); strcat(tmpstr,title); strcat(tmpstr,"]"); if( strncmp(tmpstr,szLine,strlen(tmpstr)) == 0 ) { //找到title flag = 1; } } } } fclose(fp); return ""; } //從INI文件讀取整類型數(shù)據(jù) int GetIniKeyInt(char *title,char *key,char *filename) { return atoi(GetIniKeyString(title,key,filename)); } int main(int argc, char* argv[]) { char buf[MAX_PATH]; memset(buf,0,sizeof(buf)); GetCurrentPath(buf,CONF_FILE_PATH); strcpy(g_szConfigPath,buf); int iCatAge; char szCatName[32]; iCatAge = GetIniKeyInt("CAT","age",g_szConfigPath); strcpy(szCatName,GetIniKeyString("CAT","name",g_szConfigPath)); return 0; }
下邊是配置文件:
[CAT] age=2 name=Tom
如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
上一篇:C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之模式匹配字符串定位問(wèn)題
欄 目:C語(yǔ)言
下一篇:C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)遞歸之斐波那契數(shù)列
本文標(biāo)題:C 讀取ini文件的實(shí)例詳解
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/1060.html
您可能感興趣的文章
- 01-10fatal error LNK1104: 無(wú)法打開(kāi)文件“l(fā)ibc.lib”的解決方法
- 01-10顯示任何進(jìn)程加載的DLL文件的代碼
- 01-10深入探討linux下進(jìn)程的最大線程數(shù)、進(jìn)程最大數(shù)、進(jìn)程打開(kāi)的文
- 01-10使用Inotify 監(jiān)控目錄與文件的方法詳解
- 01-10用c 獲取文件MD5值的實(shí)現(xiàn)方法
- 01-10用C實(shí)現(xiàn)添加和讀取配置文件函數(shù)
- 01-10在vs2010中,輸出當(dāng)前文件路徑與源文件當(dāng)前行號(hào)的解決方法
- 01-10C++中簡(jiǎn)單讀寫(xiě)文本文件的實(shí)現(xiàn)方法
- 01-10C語(yǔ)言文件操作函數(shù)大全(超詳細(xì))
- 01-10深入探討:linux中遍歷文件夾下的所有文件


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
- 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)
- 04-02c語(yǔ)言用函數(shù)寫(xiě)分段 用c語(yǔ)言表示分段
- 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)
- 04-02c語(yǔ)言編寫(xiě)函數(shù)冒泡排序 c語(yǔ)言冒泡排
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段
- 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎
- 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求
隨機(jī)閱讀
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10delphi制作wav文件的方法
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置