Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
分段错误解析json-c_C_Segmentation Fault_Json C - Fatal编程技术网

分段错误解析json-c

分段错误解析json-c,c,segmentation-fault,json-c,C,Segmentation Fault,Json C,当我尝试从json文件中获取val的结果时,我得到了一个seg错误。如果我注释掉这些行,其他获取值返回的结果很好 /*parseText(obj, "val", &tmp); strcpy (test.val, tmp); printf("val = %s\n", test.val); */ 这是唯一一个不是整数的,我认为我处理得不对 下面是我的代码和json文件 #include "stdio.h" #include "string.h" #include "json-c/json.

当我尝试从json文件中获取val的结果时,我得到了一个seg错误。如果我注释掉这些行,其他获取值返回的结果很好

/*parseText(obj, "val", &tmp);
strcpy (test.val, tmp);
printf("val = %s\n", test.val); */
这是唯一一个不是整数的,我认为我处理得不对

下面是我的代码和json文件

#include "stdio.h"
#include "string.h"
#include "json-c/json.h"

typedef struct __attribute__ ((packed))
{
    char val[4];
    uint32_t fetch_1;
    uint32_t fetch_2;
    uint32_t fetch_3;
    uint32_t fetch_4;
    uint32_t fetch_5;
}
TEST;

static int parseText(struct json_object *new_obj, const char *field,
                        void *val)
{
    struct json_object *o = NULL;
    json_type type;
    int ret = 0;

    if (!json_object_object_get_ex(new_obj, field, &o))
        printf("Field %s does not exist\n", field);

    type = json_object_get_type(o);

    switch (type) {
    case json_type_null:
        printf("Return type is NULL\n");
        ret = -1;
        break;
    case json_type_boolean:
        *((int *)(val)) = json_object_get_boolean(o);
        break;
    case json_type_double:
        *((double *)(val)) = json_object_get_double(o);
        break;
    case json_type_int:
        *((int *)(val)) = json_object_get_int(o);
        break;
    case json_type_string:
        val = (char *)json_object_get_string(o);
        break;
    case json_type_object:
    case json_type_array:
        ret = -1;;
        break;
    }
    return ret;

}
static inline int parsing(struct json_object *obj) {
    char* tmp = NULL;
    TEST test;
    memset(&test, 0, sizeof(test));

    parseText(obj, "val", &tmp);
    strcpy (test.val, tmp);
    printf("val = %s\n", test.val);
    parseText(obj, "fetch 1", &test.fetch_1);
    printf("fetch_1= %d\n", test.fetch_1);
    parseText(obj, "fetch 2", &test.fetch_2);
    printf("fetch_2= %d\n", test.fetch_2);
    parseText(obj, "fetch 3", &test.fetch_3);
    printf("fetch_3= %d\n", test.fetch_3);
    parseText(obj, "fetch 4", &test.fetch_4);
    printf("fetch_4= %d\n", test.fetch_4);
    parseText(obj, "fetch 5", &test.fetch_5);
    printf("fetch_5= %d\n", test.fetch_5);

    return 0;
}

char* file_read (const char* filename) {
  FILE* fp;
  char* buffer;
  long  fsize;

  /* Open the file */
  fp = fopen (filename, "r");

  if (fp == NULL)
    {
      return NULL;
    }

  /* Get the size of the file */
  fseek (fp, 0, SEEK_END);
  fsize = ftell (fp) + 1;
  fseek (fp, 0, SEEK_SET);

  /* Allocate the buffer */
  buffer = calloc (fsize, sizeof (char));

  if (buffer == NULL)
    {
      fclose (fp);
      return NULL;
    }

  /* Read the file */
  fread (buffer, sizeof (char), fsize, fp);

  /* Close the file */
  fclose (fp);

  /* Return the buffer */
  return buffer;
}

int main (int argc, char *argv[])
{
    struct json_object *jsonObj;
    int i = 0;

    char* file = file_read(argv[1]);

    jsonObj = json_tokener_parse(file);

    for(i = 0; i < json_object_array_length(jsonObj); i++) {
        json_object *obj = json_object_array_get_idx(jsonObj, i);
        parsing(obj);
    }

    return 0;
}

字段
“val”
上的parseText失败的原因是-

val = (char *)json_object_get_string(o);
这将更改局部变量
val
。这不会影响来自调用者的tmp

你需要的是

*(char**) val = (char*) json_object_get_string(o);
这会将
tmp
更改为指向由
json\u object\u get\u string
返回的字符串


在您的情况下,
tmp
NULL
,您试图从
NULL
复制,这是未定义的行为,也是导致SEG故障的原因。

字段
“val”
解析文本失败的原因是-

val = (char *)json_object_get_string(o);
这将更改局部变量
val
。这不会影响来自调用者的tmp

你需要的是

*(char**) val = (char*) json_object_get_string(o);
这会将
tmp
更改为指向由
json\u object\u get\u string
返回的字符串


在您的情况下,
tmp
NULL
,您试图从
NULL
复制,这是未定义的行为,也是导致SEG故障的原因。

您的调试方法有点粗糙。确保生成包含调试符号的程序,并在调试器(如gdb)中运行该程序。这将允许您捕获segfault,找到它发生的确切位置,并检查相关变量的值以确定最接近的原因。您的调试方法有点粗糙。确保生成包含调试符号的程序,并在调试器(如gdb)中运行该程序。这将允许您捕获SEGFULT,找到它发生的确切位置,并检查相关变量的值以确定近端原因。