#include#include int parse_line(char *buf) { char *c = NULL; char *para = NULL; char *key = NULL; char *value = NULL; if (!buf) { return -1; } c = strchr(buf, '='); if (c) { *c = 0; para = buf; key = c + 1; } c = strchr(key, '='); if (c) { *c = 0; key = key; value = c +1; printf("para:%s, buf:%s, key:%s, value:%sn", para, buf, key, value); } return 0; } int parse_config(FILE *fp) { char buf[4096] = {0}; if (fp) { while (!feof(fp)) { memset(buf, 0, 4096); if (!fgets(buf, 4096, fp)) { break; } parse_line(buf); } } return 0; } int main(int argc, char *argv[]) { FILE *fp = NULL; fp = fopen("test.txt", "r"); if (fp) { printf("Open file test.txt.n"); parse_config(fp); fclose(fp); } return 0; }
示例解析文件:
cat test.txt
Para=platform=arm



