2016-05-23 6 views
0

JsonCppライブラリを使用してJsonファイルを解析しようとしています。私はそれを解決できない問題に直面しています。私は1つのファイルを解析しているときに、以下に示すコードは完全に動作していますが、ディレクトリ内のファイルを繰り返し処理する部分を追加すると、プログラムがクラッシュします。Jsonファイルを1つずつ解析する

最初の関数は、特定のディレクトリでJsonファイルを検索し、その名前を文字列(結果)のベクトルで保存するために使用されます。

main関数では、プログラムは、必要な拡張子(.json)を定義してから、検索関数を呼び出して開始します。その後、私はそれを解析するために各ファイルを開こうとしました。

最後に、ありがとうと私は本当に助けに感謝します。 JSONライブラリにアクセスすることなく

#include "jsoncpp.cpp" 
#include <stdio.h> 
#include "json.h" 
#include <iostream> 
#include <fstream> 
#include <string> 
#include <cstdio> 
#include <cstring> 
#include <unistd.h> 
#include <dirent.h> 
#include <vector> 

using namespace std; 



vector<string> results;    // holds search results 

// recursive search algorithm 
void search(string curr_directory, string extension){ 

    DIR* dir_point = opendir(curr_directory.c_str()); 
    dirent* entry = readdir(dir_point); 
    while (entry){         // if !entry then end of directory 
     if (entry->d_type == DT_DIR){    // if entry is a directory 
      string fname = entry->d_name; 
      if (fname != "." && fname != "..") 
       search(entry->d_name, extension); // search through it 
     } 
     else if (entry->d_type == DT_REG){  // if entry is a regular file 
      string fname = entry->d_name; // filename 
               // if filename's last characters are extension 
      if (fname.find(extension, (fname.length() - extension.length())) != string::npos) 
       results.push_back(fname);  // add filename to results vector 
     } 
     entry = readdir(dir_point); 
    } 
    return; 
} 

// 
// 
// 
// 


int main(int argc, char *argv[]) 
{ 

// read Files list 

    string extension; // type of file to search for 
    extension = "json"; 

    // setup search parameters 
    string curr_directory = "/Users/ITSGC_Ready2Go/3dMap"; 

    search(curr_directory, extension); 




// loop over files 
    //if (results.size()){ 
    //std::cout << results.size() << " files were found:" << std::endl; 
    for (unsigned int z = 0; z < results.size(); ++z){ // used unsigned to appease compiler warnings 

// Opening the file using ifstream function from fstream library 
    cout <<results[z].c_str()<<endl; 
    Json::Value obj; 
    Json::Reader reader; 

    ifstream test(results[z].c_str()); 
    //test.open (results[z].c_str(), std::fstream::in); 


// Selection objects inside the file 

    reader.parse(test,obj); 

    //test >> obj; 

// Parsing ID object and returning its value as integer  
    // cout << "id :" << stoi(obj["id"].asString()) <<endl; 

// Parsing Line object with its internal objects 

    const Json::Value& lines = obj["lines"]; 

    for (int i=0; i<lines.size();i++){ 

    cout << "index : " << i << endl; 
    cout << "id:" << lines[i]["id"].asString() <<endl; 
    cout << "type:" << lines[i]["type"].asString() <<endl; 
    cout << "function:" << lines[i]["function"].asString() <<endl; 
    cout << "color:" << lines[i]["color"].asString() <<endl; 

    const Json::Value& poly = lines[i]["polyPoints"]; 

    for (int j=0; j<poly.size();j++){ 

    cout << "batch#"<<j<<endl; 
    cout << "latitude" << poly[j]["latitude"].asFloat()<<endl; 
    cout << "longitude" << poly[j]["longitude"].asFloat()<<endl; 
    cout << "altitude" << poly[j]["altitude"].asFloat()<<endl; 

    } 

    } 



// Reading the OccupancyGrid object 

// OccupancyGrid object is copied into constant to parse the arrays inside 

    const Json::Value& occupancyGrid = obj["occupancyGrid"]; 
    cout << occupancyGrid.size() <<endl; 

// The size of occupancyGrid is the used as number of iterations (#of rows) 

    for (int l=0; l<occupancyGrid.size();l++){ 

// Arrays inside occupancyGrid are copied into constant to parse the elements inside each array 

    const Json::Value& element = occupancyGrid[l]; 

// iterations over the size of the array in order to parse every element 

     cout << "row" << l << "--> "; 
     for (int k=0;k<element.size();k++){ 

      cout << element[k].asFloat(); 
      if(k<element.size()-1){ cout<< ",";} 

      } 
     cout << endl; 
    } 

// Parsing roadSigns object as found in the file 
// Need to understand the difference between format in the mail and the 1456 file 

    const Json::Value& roadsigns = obj["roadSigns"]; 

    cout << "ArrayType: " << roadsigns["_ArrayType_"].asString()<<endl; 

    const Json::Value& ArraySize = roadsigns["_ArraySize_"]; 

    for(int t=0;t<ArraySize.size();t++){ 

    cout << ArraySize[t].asInt(); 
    if (t<ArraySize.size()-1){ cout << " , ";} 

    } 

    cout<< endl; 

    if (roadsigns["_ArrayData_"].asString().empty()) { 
    cout << "ArrayData: "<<roadsigns["_ArrayData_"].asFloat(); } 

    else { cout << "ArrayData: empty "; } 
    cout <<endl; 

    test.close(); 
    test.clear(); 
    cout << "Done" << endl; 
    cout << "...." << endl; 
    cout << "...." << endl; 

    } 
    //else{ 
    // std::cout << "No files ending in '" << extension << "' were found." << std::endl; 
    //} 


} 

答えて

0

私はあなたがあまりにも助けることはできませんが、潜在的なクラッシュのための最初の明白な場所はif (fname.find(extension, (fname.length() - extension.length())) != string::npos)だろう。その電話をかける前に、ファイル名が内線番号のサイズよりも長いことを確認する必要があります。

また、非常に深いディレクトリツリーの場合は、再帰に制限を設ける必要があります。すべてのOSには、ディレクトリ名とファイル名に文字種制限があります。

+0

正常に動作するようにファイル名を取得する方法を変更したとき、ありがとうございます。しかし、今はどのようにディレクトリ内のファイルのセットを反復処理できますか? – Mohamed

関連する問題