2011-10-19 32 views
0

我正在使用庫libvtemm,它具有函數write_contents。它需要一個內部緩衝區並將其輸出到一個Glib::RefPtr<Gio::OutputStream>對象。我一直在試圖找到一種方法來將Gio :: OutputStream的內容轉換爲std::string或類似的東西,這樣我就可以在其他數據結構中播放和移動數據。Gio :: OutputStream轉換爲字符串或std :: ostream

有誰知道如何構建一個Gio::OutputStream類似於std::ostream或將其內容轉換爲std::string

我看到有一個Gio::MemoryOutputStream,像這樣的東西會有用於抓取數據到std::ostream

回答

0

對於那些正在尋找答案的人來說,這裏是我想出的將控制檯緩衝區讀入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; 

希望這有助於在未來遇到類似問題的人。

相關問題