簡體   English   中英

分割錯誤讀取json文件

[英]segmentation fault reading json file

我需要閱讀json文件中包含的信息,如下所示:

{"first":10, "second":"0", "P1":"1.e-20","P2":"1000","P3":"1000","P4":"1000","P5":"1"}

由於我沒有這個問題的經驗,因此我從使用這些行下面的簡短代碼開始。 它確實可以編譯,但在執行時會給出分段錯誤。 文件general.json在同一文件夾中。 如果我注釋最后一行,則json文件中包含的信息將正確打印在屏幕上。 誰能告訴我我在做什么錯?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fstream> // fstream.h in old versions of g++
#include <iostream>  //para cout
#include <sstream>
#include <json/json.h>

using namespace std;

int main() {
struct json_object *new_json, *json_arr, *json_reg, *json_field;
string line;
stringstream jsonfile;

ifstream json("file.json", ios::in); 
{getline(json, line); do {jsonfile << line;} while (getline(json, line));}
json.close();
cout << jsonfile.str().c_str();
new_json=json_tokener_parse(jsonfile.str().c_str());
json_field=json_object_object_get(json_reg, "first");
}

您正在使用json_reg指針而不初始化它,並且該函數取消引用它。 您(最有可能)使用json-c ,其中:

  • json_object_object_get在對象上調用json_object_object_get_ex

  • json_object_object_get_ex確實會switch(jso->o_type)取消引用無效的指針

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM