简单配置文件的解析示例

938阅读 0评论2012-06-25 zhenze12345
分类:C/C++


点击(此处)折叠或打开

  1. #include <Windows.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <malloc.h>

  5. int main()
  6. {
  7.     char line[] = " name = lily ";
  8.     char *p = line;
  9.     p += strspn(p, " \t");
  10.     char *tmp = strpbrk(p, " \t=");
  11.     int key_len = tmp - p;
  12.     char *key = (char *)calloc(sizeof(char), key_len + 1);
  13.     memcpy(key, p, key_len);
  14.     p = tmp;
  15.     p += strspn(p, " \t");
  16.     if(*p++ != '=')
  17.     {
  18.         printf("line parse error\n");
  19.         return 1;
  20.     }
  21.     p += strspn(p, " \t");
  22.     if(*p == '\r' || *p == '\n')
  23.     {
  24.         printf("line parse error\n");
  25.         return 1;
  26.     }
  27.     tmp = strpbrk(p, " \t");
  28.     int value_len = tmp - p;
  29.     char *value = (char *)calloc(sizeof(char), value_len + 1);
  30.     memcpy(value, p, value_len);
  31.     printf("key = %s, value = %s\n", key, value);
  32.     free(key);
  33.     free(value);
  34.     system("pause");
  35. }

上一篇:C#获取未知实体的属性和方法
下一篇:编译目录下所有cpp文件的makefile