gpt4 book ai didi

c++ - 从 pugixml 压缩数据

转载 作者:行者123 更新时间:2023-11-30 05:04:07 28 4
gpt4 key购买 nike

我正在尝试压缩来自 pugi::xml_document 的数据。这是我试过的:

template<class T> void save(const T &object, const QString &path)
{
pugi::xml_document doc;
object.exportXML(doc);

std::ostringstream stream;
doc.save(stream);

QByteArray data = qCompress(stream.c_str(), 9);

QFile outfile(path);
outfile.write(data);
outfile.close();
}

但它不起作用,因为 doc.save() 采用 ostream 而不是 ostringstream。我如何从 xml_document 中获取格式化为字符串的树并使用 qCompress 压缩它?

最佳答案

确保在 pugiconfig.hpp 文件中注释了 no-STL 定义:

// Uncomment this to disable STL
// #define PUGIXML_NO_STL

stringstream header 包含是必需的:

 #include <sstream>

也就是说,请注意:您是在 std::ostringstream 对象上直接调用一个不存在的方法 c_str(),而不是在其底层字符串上,由 str() 返回(您的编译器输出应该类似于:no member named 'c_str' in 'std::basic_ostringstream')。

你的代码应该是这样的:

QByteArray data = qCompress(stream.str().c_str(), 9);

关于c++ - 从 pugixml 压缩数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49085013/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com