Gio::OutputStream 到 String 或 std::ostream

发布于 2024-12-11 06:21:08 字数 414 浏览 1 评论 0原文

我正在使用库 libvtemm,它有一个函数 write_contents。它需要一个内部缓冲区并将其输出到 Glib::RefPtr对象。我一直在尝试找到一种方法将 Gio::OutputStream 的内容转换为 std::string 或类似的内容,以便我可以使用内部数据并将其移动到其他数据结构。

有谁知道如何将 Gio::OutputStream 构造为 std::ostream 或将其内容转换为 std::string >?

我看到有一个 Gio::MemoryOutputStream ,这样的东西对于将数据抓取到 std::ostream 有用吗?

I am using the library libvtemm, which has a function write_contents. It takes an internal buffer and outputs it to a Glib::RefPtr<Gio::OutputStream> object. I have been trying to find a way to convert the contents of the Gio::OutputStream into a std::string or something similar so that I can play with and move around the data inside to other data structures.

Does anyone know how to either construct a Gio::OutputStream to something like a std::ostream or convert its contents into a std::string?

I see there is a Gio::MemoryOutputStream, would something like this be useful in grabbing the data to a std::ostream?

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

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

发布评论

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

评论(1

思慕 2024-12-18 06:21:08

对于那些正在寻找答案的人,以下是我将控制台缓冲区读入 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] = '\0';

// Create a string from it
ss << charBuf;

希望这对将来遇到类似问题的人有所帮助。

For those of you who are looking for an answer, here is what I have come up with to read the console buffer into a 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] = '\0';

// Create a string from it
ss << charBuf;

Hope this helps someone in the future who comes across a similar problem.

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