【问题标题】:Best way to read name value pair in c在c中读取名称值对的最佳方法
【发布时间】:2019-03-29 00:47:42
【问题描述】:

在 c 编程中从配置文件中读取名称及其值的最佳方法是什么?

示例配置文件:

NAME=xxxx
AGE=44
DOB=mmddyyyy
WORK=zzzz

这是我正在使用的代码。这是工作。但我想知道是否有更好的方法。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int getValue(char *line, char* name, char value[])
{
    char* pch = NULL;
    char* token = NULL;
    pch = strstr(line, name);

    if(pch)
    {
        token = strtok(pch, "=");

        while (token != NULL)
        {
            pch = token;
            token = strtok(NULL, "=");
        }
        pch[strcspn ( pch, "\n" )] = '\0';
        strcpy(value,pch);
        return 1;
    }
    return 0;
}

int main()
{
    FILE * fp;
    char * line = NULL;
    size_t len = 0;
    ssize_t read;
    char value[100];
    int ret = 0;

    fp = fopen("test.txt", "r");
    if (fp == NULL)
    {
        printf ("Cannot open file \n");
        return -1;
    }

    while ((read = getline(&line, &len, fp)) != -1)
    {
        ret = getValue(line,"NAME",value);
        if (ret)
        {
            printf("NAME is %s\n", value);
        }

        ret = getValue(line,"AGE",value);
        if (ret)
        {
            printf("AGE is %s\n", value);
        }
    }

    free(line);
    fclose(fp);
    return 0;
}

如果此代码有任何问题,我也很乐意听到。

【问题讨论】:

  • 您可以开始将strtok 更改为strsep,因为它是一个已弃用的功能更多信息here,使用strsep 的简单示例here
  • 最好的方法是使用明确构建的库来处理此类事情。

标签: c linux string file


【解决方案1】:

有几个问题

  • 当文件如下所示时,您的解析不正确。只要line上有这个字符串就可以找到,不管它是在值上还是在键的一部分上。
NAMEX=xxxx
AGEX=44
DOB=mmddyyyyAGE
WORK=zzzzAGE
  • 使用 strtok line 内容将被更改。事实上,当你第二次调用getValue时,line的内容与文件不同。
AGE=NAMEzzzz=1=2
  • 从性能上看,可以直接使用line子串,不用strcpy out

建议先解析key和value,然后多次比较你要找的key。以下代码仅供参考

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

char *trim(char *str)
{
    char *start = str;
    char *end = str + strlen(str);

    while(*start && isspace(*start))
        start++;

    while(end > start && isspace(*(end - 1)))
        end--;

    *end = '\0';
    return start;
}

int parse_line(char *line, char **key, char **value)
{
    char *ptr = strchr(line, '=');
    if (ptr == NULL)
        return -1;

    *ptr++ = '\0';
    *key = trim(line);
    *value = trim(ptr);

    return 0;
}

int main()
{
    FILE *fp;
    char *line = NULL;
    size_t len = 0;
    ssize_t read;
    char *key, *value;

    fp = fopen("test.txt", "r");
    if (fp == NULL) {
        printf ("Cannot open file \n");
        return -1;
    }

    while ((read = getline(&line, &len, fp)) != -1) {
        if (parse_line(line, &key, &value))
            continue;

        if (strcmp(key, "NAME") == 0) {
            printf("NAME is %s\n", value);
        } else if (strcmp(key, "AGE") == 0) {
            printf("AGE is %s\n", value);
        }
    }

    free(line);
    fclose(fp);

    return 0;
}

【讨论】:

    猜你喜欢
    • 2012-01-11
    • 1970-01-01
    • 1970-01-01
    • 2014-09-04
    • 1970-01-01
    • 2014-12-29
    • 1970-01-01
    • 2016-08-25
    • 1970-01-01
    相关资源
    最近更新 更多