2012-12-03 2 views
5

설명서에 실제로 언급되지 않습니다.Boost.PropertyTree의 구문 분석/읽기 오류를 감지하는 방법은 무엇입니까?

나는 그것을 ifstream에 건네 줄 수 있으므로 열려 있는지 확인하여 그 경우가 대부분 처리됩니다.

하지만 boost :: property_tree :: ini_parser :: read_ini (ifstream_object, property_tree_object)를 수행 할 때;

파일의 형식이 잘못된 경우 어떻게 감지합니까? 과 같은 진단 정보를 얻는 방법이 있습니까? 구문 분석에 실패했습니다.

답변

9

예외를 catch하십시오. Base PropertyTree 예외 클래스는 std::runtime_error에서 파생 된 boost::property_tree::ptree_error이며 두 개의 자손 인 ptree_bad_dataptree_bad_path이 있습니다.

예 :

#include <boost/property_tree/ini_parser.hpp> 
#include <boost/property_tree/ptree.hpp> 
#include <iostream> 
#include <sstream> 

int main() 
{ 
    using namespace std; 
    using namespace boost; 
    using namespace property_tree; 

    stringstream ss; 
    ss << "good = value" << endl; 
    ss << "bad something" << endl; 
    try 
    { 
     ptree root; 
     read_ini(ss, root); 
    } 
    catch(const ptree_error &e) 
    { 
     cout << e.what() << endl; 
    } 
} 

출력된다 :

<unspecified file>(2): '=' character not found in line 
관련 문제