简体   繁体   中英

Gio::OutputStream to a String or 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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM