用 C parse INI 配置文件

用 C parse INI 配置文件,读取 VAL 值

一、ini配置文件的格式

为什么要用INI文件?如果我们程序没有任何配置文件时,这样的程序对外是全封闭的,一旦程序需要修改一些参数必须要修改程序代码本身并重新编译,这样很不好,所以要用配置文件,让程序发布后还能根据需要进行必要的配置;配置文件有很多如INI配置文件,XML配置文件,还有就是可以使用系统注册表等。
本文主要是为读者在实现读写INI配置文件模块之前,提供有关INI文件的格式信息。在早期的windows桌面系统中主要是用INI文件作为系统的配置文件,从win95以后开始转向使用注册表,但是还有很多系统配置是使用INI文件的。其实INI文件就是简单的text文件,只不过这种txt文件要遵循一定的INI文件格式。现在的WINCE系统上也常常用INI文件作为配置文件,这次研究INI文件的目的就是为了 给我的对接报警服务程序添加配置文件。“.INI ”就是英文 “initialization”的头三个字母的缩写;当然INI file的后缀名也不一定是".ini"也可以是".cfg",".conf ”或者是".txt"。

1、INI文件由节、键、值组成。


  [section]

参数(键=值)
  name=value

注解
  注解使用分号表示(;)。在分号后面的文字,直到该行结尾都全部为注解。

2、INI文件的格式很简单,最基本的三个要素是:sections,parameters和comments。

什么是sections ?
所有的parameters都是以sections为单位结合在一起的。所有的section名称都是独占一行,并且sections名字都被方括号包围着([ and ])。在section声明后的所有parameters都是属于该section。对于一个section没有明显的结束标志符,一个section的开始就是上一个section的结束,或者是end of the file。Sections一般情况下不能被nested,当然特殊情况下也可以实现sections的嵌套。
section如下所示:
[section]

什么是parameters?
INI所包含的最基本的“元素”就是parameter;每一个parameter都有一个name和一个value,name和value是由等号“=”隔开。name在等号的左边。
例子如下:
name = value

什么是comments?
在INI文件中注释语句是以分号“;”开始的。所有的所有的注释语句不管多长都是独占一行直到结束的,在分号和行结束符之间的所有内容都是被忽略的。
注释实例如下:
;comments text

参考文章:

https://blog.csdn.net/chexlong/article/details/6818017

二、代码参考如下:

  1 #define CONF_FILE_PATH    "Config.ini"
  2  
  3 #include <string.h>
  4  
  5 #ifdef WIN32
  6 #include <Windows.h>
  7 #include <stdio.h>
  8 #else
  9  
 10 #define  MAX_PATH 260
 11  
 12 #include <unistd.h>
 13 #include <fcntl.h>
 14 #include <stdio.h>
 15 #include <stdlib.h>
 16 #include <stdarg.h>
 17 #endif
 18  
 19 char g_szConfigPath[MAX_PATH];
 20  
 21 //获取当前程序目录
 22 int GetCurrentPath(char buf[],char *pFileName)
 23 {
 24 #ifdef WIN32
 25     GetModuleFileName(NULL,buf,MAX_PATH); 
 26 #else
 27     char pidfile[64];
 28     int bytes;
 29     int fd;
 30  
 31     sprintf(pidfile, "/proc/%d/cmdline", getpid());
 32  
 33     fd = open(pidfile, O_RDONLY, 0);
 34     bytes = read(fd, buf, 256);
 35     close(fd);
 36     buf[MAX_PATH] = '\0';
 37  
 38 #endif
 39     char * p = &buf[strlen(buf)];
 40     do 
 41     {
 42         *p = '\0';
 43         p--;
 44 #ifdef WIN32
 45     } while( '\\' != *p );
 46 #else
 47     } while( '/' != *p );
 48 #endif
 49  
 50     p++;
 51  
 52     //配置文件目录
 53     memcpy(p,pFileName,strlen(pFileName));
 54     return 0;
 55 }
 56  
 57 //从INI文件读取字符串类型数据
 58 char *GetIniKeyString(char *title,char *key,char *filename) 
 59 { 
 60     FILE *fp; 
 61     char szLine[1024];
 62     static char tmpstr[1024];
 63     int rtnval;
 64     int i = 0; 
 65     int flag = 0; 
 66     char *tmp;
 67  
 68     if((fp = fopen(filename, "r")) == NULL) 
 69     { 
 70         printf("have   no   such   file \n");
 71         return ""; 
 72     }
 73     while(!feof(fp)) 
 74     { 
 75         rtnval = fgetc(fp); 
 76         if(rtnval == EOF) 
 77         { 
 78             break; 
 79         } 
 80         else 
 81         { 
 82             szLine[i++] = rtnval; 
 83         } 
 84         if(rtnval == '\n') 
 85         { 
 86 #ifndef WIN32
 87             i--;
 88 #endif    
 89             szLine[--i] = '\0';
 90             i = 0; 
 91             tmp = strchr(szLine, '='); 
 92  
 93             if(( tmp != NULL )&&(flag == 1)) 
 94             { 
 95                 if(strstr(szLine,key)!=NULL) 
 96                 { 
 97                     //注释行
 98                     if ('#' == szLine[0])
 99                     {
100                     }
101                     else if ( '\/' == szLine[0] && '\/' == szLine[1] )
102                     {
103                         
104                     }
105                     else
106                     {
107                         //找打key对应变量
108                         strcpy(tmpstr,tmp+1); 
109                         fclose(fp);
110                         return tmpstr; 
111                     }
112                 } 
113             }
114             else 
115             { 
116                 strcpy(tmpstr,"["); 
117                 strcat(tmpstr,title); 
118                 strcat(tmpstr,"]");
119                 if( strncmp(tmpstr,szLine,strlen(tmpstr)) == 0 ) 
120                 {
121                     //找到title
122                     flag = 1; 
123                 }
124             }
125         }
126     }
127     fclose(fp); 
128     return ""; 
129 }
130  
131 //从INI文件读取整类型数据
132 int GetIniKeyInt(char *title,char *key,char *filename)
133 {
134     return atoi(GetIniKeyString(title,key,filename));
135 }
136  
137 int main(int argc, char* argv[])
138 {
139     char buf[MAX_PATH];
140     memset(buf,0,sizeof(buf));
141     GetCurrentPath(buf,CONF_FILE_PATH);
142     strcpy(g_szConfigPath,buf);
143  
144     int iCatAge;
145     char szCatName[32];
146     
147     iCatAge = GetIniKeyInt("CAT","age",g_szConfigPath);
148     strcpy(szCatName,GetIniKeyString("CAT","name",g_szConfigPath));
149  
150     return 0;
151 }

Config.ini 文件:

[CAT]
age    = 2
name = Tom

 

posted @ 2021-12-18 11:58  西门吹雪~~~  阅读(60)  评论(0编辑  收藏  举报