C语言解析文件内容,获取key、value

4 篇文章 0 订阅
3 篇文章 0 订阅
#include<stdio.h>
#include<string.h>

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:%s\n", 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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
解析JSON文件,可以使用json-c库,具体步骤如下: 1. 引入json-c库的头文件 ```c #include <json-c/json.h> ``` 2. 读取JSON文件内容 ```c FILE *fp = fopen("filename.json", "r"); if (fp == NULL) { printf("Failed to open file\n"); return -1; } char buffer[1024]; fread(buffer, 1, 1024, fp); fclose(fp); ``` 3. 解析JSON文件内容 ```c struct json_object *json_obj = json_tokener_parse(buffer); ``` 4. 获取JSON对象的 ```c // 获取字符串类型的 const char *str_value; json_object_object_get_ex(json_obj, "key", &str_value); printf("%s\n", str_value); // 获取整数类型的 int int_value; json_object_object_get_ex(json_obj, "key", &int_value); printf("%d\n", int_value); // 获取数组类型的 struct json_object *arr_obj; json_object_object_get_ex(json_obj, "key", &arr_obj); int arr_len = json_object_array_length(arr_obj); for (int i = 0; i < arr_len; i++) { struct json_object *item = json_object_array_get_idx(arr_obj, i); // 处理数组项 } ``` 完整的示例代码如下: ```c #include <stdio.h> #include <json-c/json.h> int main() { FILE *fp = fopen("example.json", "r"); if (fp == NULL) { printf("Failed to open file\n"); return -1; } char buffer[1024]; fread(buffer, 1, 1024, fp); fclose(fp); struct json_object *json_obj = json_tokener_parse(buffer); const char *str_value; json_object_object_get_ex(json_obj, "name", &str_value); printf("name: %s\n", str_value); int age_value; json_object_object_get_ex(json_obj, "age", &age_value); printf("age: %d\n", age_value); struct json_object *hobby_arr_obj; json_object_object_get_ex(json_obj, "hobbies", &hobby_arr_obj); int hobby_arr_len = json_object_array_length(hobby_arr_obj); printf("hobbies:\n"); for (int i = 0; i < hobby_arr_len; i++) { struct json_object *item = json_object_array_get_idx(hobby_arr_obj, i); printf("- %s\n", json_object_get_string(item)); } json_object_put(json_obj); return 0; } ``` 需要注意的是,使用完json对象后,需要调用`json_object_put`函数释放内存。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值