当INI文件不存在时,使用Boost属性树读取INI文件
Reading INI file using Boost Property Tree when ini file not exist
我正在使用Boost.PropertyTree
加载INI文件:
read_ini( INI_FILE_NAME, pt );
若ini文件不存在,则Boost
引发异常。
如何在不出现异常的情况下读取ini
文件,但却得到它不存在的信息?
你不能。您必须处理所有异常,并选择要使用的异常/显示异常。
try
{
read_ini( INI_FILE_NAME, pt );
}
catch( std::exception &ex )
{
// you either print it out or have a MessageBox pop up or hide it.
std::cerr << ex.what( ) << std::endl;
}
只需相应地处理异常即可。
按如下结构构建代码以正确处理异常
try
{
read_ini(INI_FILE_NAME, pt);
//Do something with pt
}
catch(ptree_bad_data& ex)
{
// Log or error string stating that the data read is corrupt
}
catch(ptree_bad_path& ex)
{
// Log or error string stating that there was problem in
// accessing the INI at the given location.
}
catch(ptree_error& ex)
{
// Log or error string stating generic ptree exception.
}
catch(...)
{
// Log or error string stating a generic exception.
// Might want to rethrow the exception to address it correctly.
//throw;
}
。.
这将处理您的异常,避免尝试使用未填充的pt
,并告诉您过程中发生的确切问题,同时允许您的代码继续而不中止。
相关文章:
- Ini 文件类型名称 预期C++
- 我们可以在INI文件中使用 #ifdef 宏,或者我们如何限制INI文件中写入的设置的编译?
- 尝试从C++中的 ini 文件读取
- boost::P rogram_options 一种判断值是从命令行还是 ini 文件的方法?
- 如何完整读取 INI 文件
- 写入.ini文件 - SimpleIni SetValue 尽管看起来成功了,但什么也没做
- GetPrivateprofilestring 不会获取当前目录中的 ini 文件
- 使用64编译器在C 中读取INI文件
- 带有qt的.ini文件中的奇怪双重值
- c++ GetPrivateProfileString 从当前目录读取 ini 文件
- C ++ simpleini 示例,用于从目录中获取 ini 文件
- 读取 ini 文件C++错误
- 如何使用 POCO 库将更改写入".ini"文件?
- WinAPI - 如果文件不存在,则创建.ini文件
- 如何使用 Boost 解析超过 2 级的 ini 文件
- 在不使用boost覆盖整个ini的情况下写入ini文件值
- 在运行时使用C++/boost从资源中解析.ini文件
- 从ini文件中读取十六进制字符
- 如何在读取 ini 文件时使用扩展字符集?(C++郎)
- 在 dll 文件中包含一个 INI 文件