如何检测Boost.PropertyTree的解析/读取失败? [英] How to detect parse/read failure of Boost.PropertyTree?

查看:453
本文介绍了如何检测Boost.PropertyTree的解析/读取失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档并没有真正说明.

我知道我可以将它交给ifstream,所以我可以检查一下以确保它是打开的,这样大多数情况下都可以解决.

I get that I can hand it an ifstream, so I could check to make sure it's open, so that case is mostly dealt with.

但是在进行boost :: property_tree :: ini_parser :: read_ini(ifstream_object,property_tree_object);

But when doing boost::property_tree::ini_parser::read_ini(ifstream_object,property_tree_object);

如何检测文件格式是否错误?我有什么方法可以获取诊断信息,例如 where 解析失败?

How do I detect if the file was in a bad format? Is there any way for me to get diagnostic information, such as where the parse failed?

推荐答案

仅捕获异常.基本PropertyTree异常类是 boost::property_tree::ptree_error ,它源自 std::runtime_error ,它有两个后代: ptree_bad_path .

Just catch exceptions. Base PropertyTree exception class is boost::property_tree::ptree_error which derives from std::runtime_error, and it has two descendants: ptree_bad_data and ptree_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

这篇关于如何检测Boost.PropertyTree的解析/读取失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆