输出流转换为String或std::ostream
Gio::OutputStream to a String or std::ostream
我正在使用库libvtemm,它有一个函数write_contents
。它接受一个内部缓冲区并将其输出到一个Glib::RefPtr<Gio::OutputStream>
对象。我一直在试图找到一种方法将Gio::OutputStream的内容转换为std::string
或类似的东西,以便我可以玩和移动内部的数据到其他数据结构。
有没有人知道如何将Gio::OutputStream
构建为std::ostream
或将其内容转换为std::string
?
我看到有一个Gio::MemoryOutputStream
,这样的东西在抓取数据到std::ostream
是有用的吗?
对于那些正在寻找答案的人,这里是我提出的将控制台缓冲区读取为std::string
的方法。
// Create a mock stream just for this example
Glib::RefPtr<Gio::MemoryOutputStream> bufStream =
Gio::MemoryOutputStream::create(NULL, 0, &realloc, &free);
// Create the stringstream to use as an ostream
std::stringstream ss;
// Get the stream size so we know how much to allocate
gsize streamSize = bufStream->get_data_size();
char *charBuf = new char[streamSize+1];
// Copy over the data from the buffer to the charBuf
memcpy(charBuf, bufStream->get_data(), streamSize);
// Add the null terminator to the "string"
charBuf[streamSize] = ' ';
// Create a string from it
ss << charBuf;
希望这对将来遇到类似问题的人有所帮助。
相关文章:
- boost::interprocess::file_lock 与 std::ostream 一起使用时的错误行为
- 与'operator<<'不匹配(操作数类型'std::ostream {aka std::basic_ostream<char>}'
- 与"operator<<"不匹配(操作数类型'std::ostream'
- 运算符的歧义错误<<自定义 std::ostream 子类中的重载
- 关于 std::ostream 构造函数
- STD :: Ostream忽略了通过setf()在基础上设置的十六进制标志
- 将 std::ostream 转换为 std::string
- STD :: Ostream:类是不可接受的[C ]
- 为什么在三元运算符中使用STD :: Ostream不会编译
- 当我编译引用 std::ostream 时,我有一个奇怪的错误弹出
- 如何跟踪写入std :: Ostream对象的字节数
- std::ostream的下降表达(针对SFINAE)
- BOOST ::变体无法解决运算符&lt;&lt;对于STD :: Ostream
- 我什么时候应该返回 std::ostream
- 如何移动std::ostream
- 如何在自定义 std::ostream 类中使用 std::endl
- 如何清除std::ostream
- 返回对此的引用时出现意外的std::ostream输出
- std::ostream to QDataStream
- 使用 std::ostream 方法将格式化文本输出到 std::string 对象