【问题标题】:Interfacing with a vector<char> as a stream using binary write() read()使用二进制 write() read() 将 vector<char> 作为流接口
【发布时间】:2017-05-16 03:39:23
【问题描述】:

我目前正在研究将二进制数据写入std::vector&lt;char&gt; 的可移植、惯用、仅标准库的 C++ 方法。我想以与任何其他二进制 ostream 相同的方式与容器交互,使用 write() 函数。

到目前为止,我唯一出现的就是继承自 std::basic_streambuf&lt;&gt;。有没有更好的替代品?

【问题讨论】:

  • 我记得有人问过std::string 的类似问题。不知道my answer 是否对您有帮助,但还是看看吧。
  • 您可能会在此处找到有用的 char_array_buffer 示例:mr-edd.co.uk/blog/beginners_guide_streambuf
  • 为什么是vectorstd::string 还不够好?
  • 所以你有 std::stringstream 与 std::string 一起使用
  • "写入函数似乎在跳过字节" 写入函数不会跳过任何内容。如果您在使用 stringstream 时遇到问题,请提出问题。

标签: c++


【解决方案1】:

你是对的,从std::basic_streambuf 继承是这样做的方法。不确定你是否感兴趣,但是 boost::iostreams 和 boost::interprocess 已经实现了这种类型的东西:

http://www.boost.org/doc/libs/1_64_0/libs/iostreams/doc/index.html http://www.boost.org/doc/libs/1_64_0/doc/html/interprocess/streams.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    相关资源
    最近更新 更多