【问题标题】:Gio::OutputStream to a String or std::ostreamGio::OutputStream 到 String 或 std::ostream
【发布时间】:2011-10-19 18:19:40
【问题描述】:

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

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

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

【问题讨论】:

    标签: c++ glib gtkmm


    【解决方案1】:

    对于那些正在寻找答案的人,这是我想出的将控制台缓冲区读入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;
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 2015-09-04
      • 2021-01-02
      • 2010-09-26
      • 1970-01-01
      • 2018-08-10
      相关资源
      最近更新 更多