6

文档并没有真正说明。

我知道我可以给它一个 ifstream,所以我可以检查它以确保它是打开的,所以这种情况主要是处理的。

但是当做 boost::property_tree::ini_parser::read_ini(ifstream_object,property_tree_object);

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

4

1 回答 1

12

只捕获异常。基础 PropertyTree 异常类boost::property_tree::ptree_error派生自std::runtime_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
于 2013-07-10T17:42:28.067 回答