C/C++读写配置文件

用C写了个加载配置文件的demo简单程序, 按行支持 key=val 的格式以及行注释/文本注释等。

以下贴下代码, 纯当笔记:

[cpp]  view plain  copy
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4.   
  5. #define MAX_BUF_LEN 1024  
  6. #define MAX_KEY_LEN 64  
  7. #define MAX_VAL_LEN 256  
  8.   
  9. int Trim(char s[])  
  10. {  
  11.     int n;  
  12.     for(n = strlen(s) - 1; n >= 0; n--)  
  13.     {  
  14.         if(s[n]!=' ' && s[n]!='\t' && s[n]!='\n')  
  15.             break;  
  16.         s[n+1] = '\0';  
  17.     }  
  18.     return n;  
  19. }  
  20.   
  21. int loadConfigDemo(const char* config_path)  
  22. {  
  23.     FILE * file = fopen(config_path, "r");  
  24.     if (file == NULL)  
  25.     {  
  26.         printf("[Error]open %s failed.\n", config_path);  
  27.         return -1;  
  28.     }  
  29.   
  30.   
  31.     char buf[MAX_BUF_LEN];  
  32.     int text_comment = 0;  
  33.     while(fgets(buf, MAX_BUF_LEN, file) != NULL)  
  34.     {  
  35.         Trim(buf);  
  36.         // to skip text comment with flags /* ... */  
  37.         if (buf[0] != '#' && (buf[0] != '/' || buf[1] != '/'))  
  38.         {  
  39.             if (strstr(buf, "/*") != NULL)  
  40.             {  
  41.                 text_comment = 1;  
  42.                 continue;  
  43.             }  
  44.             else if (strstr(buf, "*/") != NULL)  
  45.             {  
  46.                 text_comment = 0;  
  47.                 continue;  
  48.             }  
  49.         }  
  50.         if (text_comment == 1)  
  51.         {  
  52.             continue;  
  53.         }  
  54.   
  55.   
  56.         int buf_len = strlen(buf);  
  57.         // ignore and skip the line with first chracter '#', '=' or '/'  
  58.         if (buf_len <= 1 || buf[0] == '#' || buf[0] == '=' || buf[0] == '/')  
  59.         {  
  60.             continue;  
  61.         }  
  62.         buf[buf_len-1] = '\0';  
  63.   
  64.   
  65.         char _paramk[MAX_KEY_LEN] = {0}, _paramv[MAX_VAL_LEN] = {0};  
  66.         int _kv=0, _klen=0, _vlen=0;  
  67.         int i = 0;  
  68.         for (i=0; i<buf_len; ++i)  
  69.         {  
  70.             if (buf[i] == ' ')  
  71.                 continue;  
  72.             // scan param key name  
  73.             if (_kv == 0 && buf[i] != '=')  
  74.             {  
  75.                 if (_klen >= MAX_KEY_LEN)  
  76.                     break;  
  77.                 _paramk[_klen++] = buf[i];  
  78.                 continue;  
  79.             }  
  80.             else if (buf[i] == '=')  
  81.             {  
  82.                 _kv = 1;  
  83.                 continue;  
  84.             }  
  85.             // scan param key value  
  86.             if (_vlen >= MAX_VAL_LEN || buf[i] == '#')  
  87.                 break;  
  88.             _paramv[_vlen++] = buf[i];  
  89.         }  
  90.         if (strcmp(_paramk, "")==0 || strcmp(_paramv, "")==0)  
  91.             continue;  
  92.         printf("%s=%s\n", _paramk, _paramv);  
  93.     }  
  94.     return 0;  
  95. }  
  96.   
  97. int main()  
  98. {  
  99.     loadConfigDemo("./test.conf");  
  100.     return 0;  
  101. }  

其中用于测试的配置文件如下:

[plain]  view plain  copy
  1. # basic  
  2.    version=1  
  3.   
  4. // for testing line comment  
  5. # fan settings  
  6. fan_temp_level=2  
  7. fan_pre_heat=1  
  8.   
  9. /* test text comment = test text comment  
  10. test text comment = test text comment  
  11. // just for test  
  12. */  
  13. # /* just for test  
  14.   
  15.   
  16. # wash  
  17. wash_water_temp_level =   2  
  18. wash_water_pressure_level=2  
  19.   
  20. # drying  
  21. drying_pressure_level=2  
  22. drying_temp_level=2  
  23.   
  24. # light  
  25. light_threshold=500  
  26. light_rgb=4452563       # use 3bytes to store this value: R G B  
  27.   
  28. # voice  
  29. volume=45    # range from 0 to 100  
演示效果:

[plain]  view plain  copy
  1. xiaomo:/tmp# ./test  
  2. version=1  
  3. fan_temp_level=2  
  4. fan_pre_heat=1  
  5. wash_water_temp_level=2  
  6. wash_water_pressure_level=2  
  7. drying_pressure_level=2  
  8. drying_temp_level=2  
  9. light_threshold=500  
  10. light_rgb=4452563  
  11. volume=45  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值