标签:style blog http io ar color os 使用 sp
1 [protocol] ; Protocol configuration 2 version=6 ; IPv6 3 4 [user] 5 name = Bob Smith ; Spaces aroud ‘=‘ are strpped 6 email = bob@smith.com ; And comments (like this) ignored 7 active = true ; Test a boolean 8 pi = 3.14159 ; Test a floating point number
实例代码如下:
1 typedef struct 2 { 3 int version; 4 const char* name; 5 const char* email; 6 } configuration; 7 8 static int handler(void* user, const char* section, const char* name, 9 const char* value) 10 { 11 configuration* pconfig = (configuration*)user; 12 13 #define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0 14 if (MATCH("protocol", "version")) { 15 pconfig->version = atoi(value); 16 } else if (MATCH("user", "name")) { 17 pconfig->name = strdup(value); 18 } else if (MATCH("user", "email")) { 19 pconfig->email = strdup(value); 20 } else { 21 return 0; /* unknown section/name, error */ 22 } 23 return 1; 24 } 25 26 int main(int argc, char* argv[]) 27 { 28 configuration config; 29 30 if (ini_parse("test.ini", handler, &config) < 0) { 31 printf("Can‘t load ‘test.ini‘\n"); 32 return 1; 33 } 34 printf("Config loaded from ‘test.ini‘: version=%d, name=%s, email=%s\n", 35 config.version, config.name, config.email); 36 return 0; 37 }
-------------------------------------------------------------------------------
标签:style blog http io ar color os 使用 sp
原文地址:http://www.cnblogs.com/pansj/p/iniFile_Summary.html