我面临一个问题,我有一个配置文件,我用 boost::property_tree:info_parser 解析它。
我使用这段代码来完成这项工作:
struct _Config
{
std::string info[2];
boost::property_tree::ptree pt;
_Config()
{
//check if config file exists, if not create it, etc, do other stuff not related to the issue
//this code reproduces the issue
//DEFAULT VALUE, can be changed by configuration later
info[0] = "If updating this file make sure to update all settings accordingly.";
info[1] = "This program has been created by Name 'Nickname' Lastname";
}
void Save()
{
boost::property_tree::info_parser::write_info(".\\config.cfg", pt);
}
void Load()
{
boost::property_tree::info_parser::read_info(".\\config.cfg", pt);
{
//check if variable already exists in config file, if yes load it to
{
//try to load entry
boost::optional<std::string> v = pt.get_optional<std::string>("Info.a");
if (v.is_initialized())
//overwrite default value
info[0] = v.get();
}
//if entry does not exist it will be created now, else the loaded value will be saved
pt.put<std::string>("Info.a", info[0]);
}
//again for next variable
{
{
boost::optional<std::string> v = pt.get_optional<std::string>("Info.b");
if (v.is_initialized())
info[1] = v.get();
}
pt.put<std::string>("Info.b", info[1]);
}
Save();
}
~_Config()
{
Save();
pt.clear();
}
} Config;
现在,我的部分第一次看起来像这样:
Info
{
a "If updating this file make sure to update all settings accordingly."
b "This program has been created by Name 'Nickname' Lastname"
}
再次启动时我的配置在保存时变成这样:
Info
{
a If updating this file make sure to update all settings accordingly.
b This program has been created by Name 'Nickname' Lastname
}
但再次重新启动代码后,信息部分变得一团糟,我的程序崩溃了:
Info
{
a If
updating this
file make
sure to
update all
accordingly. ""
b This
program has
been created
by Name
'Nickname' Lastname
}
如何确保空格是可接受的字符并且引号不会被删除?我还注意到我在配置文件中所做的任何注释都没有被保存,是否有保存它们的选项?
我在 Windows 8 x64 的 32 位应用程序中使用 boost 1.55 和 Visual Studio 2013。