有什么方法可以使用 boost::property::ptree
在 ini 文件中写入注释吗?
类似的东西:
void save_ini(const std::string& path)
{
boost::property_tree::ptree pt;
int first_value = 1;
int second_value = 2;
// Write a comment to describe what the first_value is here
pt.put("something.first_value", );
// Write a second comment here
pt.put("something.second_value", second_value);
boost::property_tree::write_ini(path, pt);
}
文档 here不提供信息。 boost 实现了吗?
最佳答案
boost::property_tree::ptree 用于各种“属性树”类型(INFO、INI、XML、JSON 等),因此除了作为允许 key 的奇特容器外,它本身并不支持任何东西=> 值设置。你的最后一行(应该是):
boost::property_tree::ini_parser::write_ini(path, pt);
是唯一将您正在做的事情定义为 INI 而不是其他格式之一的东西。例如,您可以轻松地用写入 XML 来替换该行,它也可以工作。因此,您可以看到 property_tree::ptree 不能具有特定于特定类型文件的东西。
你能做的最好的事情就是为你的每个 child 添加一个“评论”设置——像这样:
pt.put("something.comments", "Here are the comments for the 'something' section");
您可以为任何 child 使用您想要的任何名称的属性...并在读取期间迭代时简单地忽略它们。因此,如果您愿意,没有理由不像这样发挥创意——这是您的程序!
关于c++ - 使用 boost::property_tree::ptree 将注释写入 ini 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27354838/