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文件讀取字符串類(lèi)型數(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文件讀取整類(lèi)型數(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文件讀取字符串類(lèi)型數(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文件讀取整類(lèi)型數(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ì)本站的支持!
相關(guān)文章
C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單猜數(shù)字游戲
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單猜數(shù)字游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
C語(yǔ)言計(jì)算器的3種實(shí)現(xiàn)方法代碼
這篇文章主要給大家介紹了關(guān)于C語(yǔ)言計(jì)算器的3種實(shí)現(xiàn)方法,文中通過(guò)代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一的參考借鑒價(jià)值,需要的朋友可以參考下2007-01-01
C語(yǔ)言 數(shù)據(jù)類(lèi)型詳細(xì)介紹
本文主要講解C語(yǔ)言 數(shù)據(jù)類(lèi)型,這里整理了詳細(xì)的數(shù)據(jù)類(lèi)型的資料,希望能幫助剛剛開(kāi)始學(xué)習(xí)C語(yǔ)言的同學(xué)2016-08-08
C#使用反射加載多個(gè)程序集的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇C#使用反射加載多個(gè)程序集的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06
探討:將兩個(gè)鏈表非降序合并為一個(gè)鏈表并依然有序的實(shí)現(xiàn)方法
本篇文章是對(duì)將兩個(gè)鏈表非降序合并為一個(gè)鏈表并依然有序的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
c++使用單例模式實(shí)現(xiàn)命名空間函數(shù)案例詳解
這篇文章主要介紹了c++使用單例模式實(shí)現(xiàn)命名空間函數(shù),本案例實(shí)現(xiàn)一個(gè)test命名空間,此命名空間內(nèi)有兩個(gè)函數(shù),分別為getName()和getNameSpace(),本文結(jié)合實(shí)例代碼給大家講解的非常詳細(xì),需要的朋友可以參考下2023-04-04

