当前位置: 技术问答>linux和unix
哪位大侠能告诉我在linux读写*.ini的方法
来源: 互联网 发布时间:2015-06-09
本文导语: 如题,在windows下可以用getprivateprofilestring来实现,linux有没有类似功能的函数? | /*从配置文件中取特定字符串参数*/ int GetProfileString(char *filename,char *section,char *option,char *defval,char *iRetVal) { FILE...
如题,在windows下可以用getprivateprofilestring来实现,linux有没有类似功能的函数?
|
/*从配置文件中取特定字符串参数*/
int GetProfileString(char *filename,char *section,char *option,char *defval,char *iRetVal)
{
FILE *fp;
int i,pos;
int section_len,option_len;
int in_section,is_find;
char line[2000];
char *cfgfilepath;
char cfgfilename[255];
in_section = 0;
is_find = 0;
section_len = strlen(section);
option_len = strlen(option);
if((cfgfilepath=getenv("CFG_HOME"))==NULL)
{
return -1;
}
strcpy(cfgfilename,cfgfilepath);
strcat(cfgfilename,"/");
strcat(cfgfilename,filename);
fp = fopen(cfgfilename,"r");
if(fp == NULL)
{
return -1;
}
while(!feof(fp))
{
if(fgets(line,sizeof(line),fp) == NULL)
{
break;
}
chop(line);
trim(line,' ');
/*注释*/
if(line[0] == '#')
{
continue;
}
/*空串*/
if(line[0] == '')
{
continue;
}
/*起始符*/
if(line[0] == '[')
{
if(in_section)
{
break;
}
pos = 1;
for(i=0;line[pos + i]==' ';i++);
pos += i;
for(i=0;i
int GetProfileString(char *filename,char *section,char *option,char *defval,char *iRetVal)
{
FILE *fp;
int i,pos;
int section_len,option_len;
int in_section,is_find;
char line[2000];
char *cfgfilepath;
char cfgfilename[255];
in_section = 0;
is_find = 0;
section_len = strlen(section);
option_len = strlen(option);
if((cfgfilepath=getenv("CFG_HOME"))==NULL)
{
return -1;
}
strcpy(cfgfilename,cfgfilepath);
strcat(cfgfilename,"/");
strcat(cfgfilename,filename);
fp = fopen(cfgfilename,"r");
if(fp == NULL)
{
return -1;
}
while(!feof(fp))
{
if(fgets(line,sizeof(line),fp) == NULL)
{
break;
}
chop(line);
trim(line,' ');
/*注释*/
if(line[0] == '#')
{
continue;
}
/*空串*/
if(line[0] == '')
{
continue;
}
/*起始符*/
if(line[0] == '[')
{
if(in_section)
{
break;
}
pos = 1;
for(i=0;line[pos + i]==' ';i++);
pos += i;
for(i=0;i