Linux C实现 读取ini文件项目值的功能---GetIniSectionItem


Linux C实现 读取ini文件项目值的功能---GetIniSectionItem ,以下版本又更新,可以比较正确的读取到各个项的值。
  1. #ifndef INI_FILE_H  
  2. #define INI_FILE_H  
  3.  
  4. #define NOFILE "nofile"  
  5. #define INIFILE "test.ini"  
  6. #define STRLENMAX 30  
  7. #define ITEMLENMAX 30  
  8. #define LINELENMAX 1024  
  9.  
  10. #include<string.h>  
  11.   
  12. char* GetIniSectionItem(char* FileName,char* Section,char* Item)  
  13. {  
  14. char *value,Sect[30],c;  
  15. char linebuf[1024],oldline[1024];  
  16. FILE *inifp;  
  17. int i;  
  18. strcpy(Sect,"[");  
  19. strcat(Sect,Section);  
  20. strcat(Sect,"]");  
  21. /*strcpy(Ite,"[");strcat(Ite,Item);strcat(Ite,"]");*/  
  22. if((inifp=fopen(FileName,"rb"))==NULL)  
  23. {  
  24.     return (char*)NOFILE;  
  25. }  
  26. /*printf("Sect = %s,Item = %s /n",Sect,Item);*/  
  27. while((c=fgetc(inifp))!=EOF)  
  28. {  
  29.     if(c=='[')  
  30.     {  
  31.     ungetc(c,inifp);  
  32.     fgets(linebuf,1024,inifp);  
  33.     if(strstr(linebuf,Sect))  
  34.     {  
  35.         while((c=fgetc(inifp))!='[' && c!=EOF)  
  36.         {  
  37.             ungetc(c,inifp);  
  38.             fgets(linebuf,1024,inifp);  
  39.             if(strstr(linebuf,Item))  
  40.             {  
  41.                 if(value=strchr(linebuf,'='))  
  42.                 {  
  43.                 value++;  
  44.                 /*printf("value = %s /n",value);*/  
  45.                 fclose(inifp);  
  46.                 if(*value=='/n')  
  47.                     return (char *)NULL;  
  48.                 return value;  
  49.                 }  
  50.             }  
  51.         }  
  52.         if(c==EOF)  
  53.         {  
  54.          break;  
  55.         }  
  56.         ungetc(c,inifp);  
  57.     }  
  58.     }  
  59.     else  
  60.     {  
  61.     ungetc(c,inifp);  
  62.     fgets(linebuf,1024,inifp);  
  63.     }  
  64. }  
  65. fclose(inifp);  
  66. return (char*)NULL;  
  67. }  
  68.  
  69. #endif   

以下是以前的版本,存在bug。

  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3. #include<unistd.h>  
  4. #include<string.h>  
  5.   
  6.   
  7. char* GetIniSectionItem(FILE* fp,char* Section,char* Item)  
  8. {  
  9. char *value;  
  10. char linebuf[1024];  
  11. while(fgets(linebuf,1024,fp))  
  12. {  
  13.     if(strstr(linebuf,Section))  
  14.     {  
  15.     fgets(linebuf,1024,fp);  
  16.     if(strstr(linebuf,Item))  
  17.     {  
  18.         if(value=strchr(linebuf,'='))  
  19.         {  
  20.         value++;  
  21.         printf("value = %s /n",value);  
  22.         return value;  
  23.         }  
  24.     }  
  25.     }  
  26. }  
  27. return (char*)NULL;  
  28. }  
  29.   
  30. int main(void)  
  31. {  
  32. FILE *fp;  
  33. char *value;  
  34. if((fp=fopen("test.ini","rb"))==(FILE*)0)  
  35. {  
  36. printf("open file error!/n");  
  37. exit(1);  
  38. }  
  39. value=GetIniSectionItem(fp,"TEST","NAME");  
  40. fclose(fp);  
  41. printf("value1 = %s/n",value);  
  42. return 0;  
  43. }  

大家可以实验下:

test.ini

[dddd]
dsfsd=sdfs
[TEST3]
NAMe=robertluo3
[TEST!]
nAME=robertluo1
[TESt]
NAME=robertluo2
[TEST]
NAME=robertluo

输入结果为:

value = robertluo

当项目在文件中有多个时,返回第一个项目的值。

相关内容