用C读取INI配置文件


在Windows下可以用GetPrivateProfileString或GetPrivateProfileInt方便读取.ini配置文件内容,但是在Linux平台上就一筹莫展了。为了解决该问题,打算用C来读取.ini,即可不受平台的限制了。
  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. }  

下边是配置文件:

[CAT]

age=2

name=Tom

相关内容