【问题标题】:Writing comments to ini file with boost::property_tree::ptree使用 boost::property_tree::ptree 向 ini 文件写入注释
【发布时间】:2015-02-05 22:08:02
【问题描述】:

有没有办法使用boost::property::ptree在ini文件中写入cmets?

类似的东西:

  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 实现了吗?

【问题讨论】:

    标签: c++ boost ini boost-propertytree


    【解决方案1】:

    boost::property_tree::ptree 用于各种“属性树”类型(INFO、INI、XML、JSON 等),因此除了作为允许密钥的花​​哨容器之外,它本身不支持任何东西=>值设置。你的最后一行(应该是):

    boost::property_tree::ini_parser::write_ini(path, pt);
    

    是唯一将您正在做的事情定义为 INI 而不是其他格式之一的事情。例如,您可以轻松地用写入 XML 来替换该行,它也可以工作。因此,您可以看到 property_tree::ptree 不能包含特定类型文件的特定内容。


    你能做的最好的事情就是为你的每个孩子添加一个“cmets”设置——像这样:

    pt.put("something.comments", "Here are the comments for the 'something' section");
    

    您可以为任何具有您想要的名称的任何孩子设置属性......并且在读取期间迭代时只需忽略它们。因此,如果您愿意,没有理由不进行这样的创意——这是您的程序!

    【讨论】:

    • 我考虑过,但我想我会在没有 cmets 的情况下离开它。我宁愿这样做,也不愿拥有包含 cmets 的假属性。无论如何,谢谢!
    • 我认为它不会正常工作 - 无法保证写入会遵守您想要的顺序。
    猜你喜欢
    • 2013-04-14
    • 1970-01-01
    • 2011-12-30
    • 2011-12-30
    • 2013-07-14
    • 1970-01-01
    • 2011-10-01
    相关资源
    最近更新 更多