以文件名作为键的 Boost 属性树

发布于 2024-08-16 00:05:39 字数 452 浏览 5 评论 0原文

我试图使用文件名作为 boost:: PropertyTree

然而,'.'文件名中的字符(例如“example.txt”)会导致在属性树中添加附加层。最明显的解决方案是替换“.”与另一个字符,但可能有更好的方法来做到这一点,例如使用转义字符。

在以下示例中,值 10 将被放入节点“txt”(“example”的子节点)中。相反,我希望将值 10 存储在节点“example.txt”中。

ptree pt;
pt.put("example.txt", 10);

如何使用单个节点的完整文件名?

提前感谢您的帮助!

I am trying to use filenames as the key in boost::PropertyTree

However, the '.' character in a filename such as "example.txt" causes an additional layer to be added within the property tree. The most obvious solution would be to replace '.' with another character, but there is likely a better way to do this, such as with an escape character.

In the following example, the value 10 will be put in the node 'txt', a child of 'example'. Instead, I want the value 10 to be stored in the node 'example.txt'.

ptree pt;
pt.put("example.txt", 10);

How can I use the full filename for a single node?

Thanks in advance for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最美的太阳 2024-08-23 00:05:39

只需显式插入树即可:

pt.push_back(ptree::value_type("example.txt", ptree(10)));

put 方法只是为了方便起见,这就是它自动将 . 解析为附加层的原因。像我上面所示的那样显式构造 value_type 可以避免这个问题。

解决该问题的另一种方法是在 putget 中使用额外的参数,这会更改分隔符。

pt.put('/', "example.txt", "10");
pt.get<string>('/', "example.txt");

根据记录,我以前从未使用过这个类。我从您链接到的页面直接获取了所有这些信息; )

Just insert the tree explicitly:

pt.push_back(ptree::value_type("example.txt", ptree(10)));

The put method is simply there for convenience, which is why it automatically parses . as an additional layer. Constructing the value_type explicitly like I have shown above avoids this problem.

An alternative way to solve the problem is to use an extra argument in put and get, which changes the delimeter.

pt.put('/', "example.txt", "10");
pt.get<string>('/', "example.txt");

For the record, I've never used this class before in my life. I got all this information right from the page you linked to ; )

鹤仙姿 2024-08-23 00:05:39

问题是文档已经过时了。必须按如下方式创建路径类型对象,并使用另一个对于指定为分隔符的文件路径无效的字符,如下所示:

pt.put(boost::property_tree::ptree::path_type("example.txt", '|'), 10);

我从新闻组 gmane.comp.lib.boost.devel 的 boost 邮件列表中找到了解决方案的路径由菲利普·沃彻发布。

The problem was that the documentation was outdated. A path type object must be created as follows, with another character that is invalid for file paths specified as the delimiter as follows:

pt.put(boost::property_tree::ptree::path_type("example.txt", '|'), 10);

I found a path to the solution from the boost mailing list at the newsgroup gmane.comp.lib.boost.devel posted by Philippe Vaucher.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文