【问题标题】:chaining c++ streams链接 C++ 流
【发布时间】:2009-05-04 06:07:59
【问题描述】:

我正在考虑将几个 c++ iostreams“链接”在一起以过滤输入两次。我正在使用 gzstreams 读取 zlib 压缩文件,并且正在考虑编写一个从流中读取并执行编码转换的流。也许通过将打开的流作为构造函数参数传递...您认为如何最好地完成?

【问题讨论】:

  • “链接”是什么意思?
  • 正如 eed3si9n 发布的,很像 boost::iostreams 过滤器。但我想知道通过将先前打开的流传递给从 public std::streambuf 或其他方法派生的自定义类构造函数来自己完成是否是一个好的设计。

标签: c++ iostream


【解决方案1】:

我没有使用过这个,但是 boost 的 filtering_stream 可能会有所帮助。

作为一个例子,我发现a mailing list postindent.hpp,它实现了一个缩进输出的输出过滤器:

boost::iostreams::filtering_ostream out; 
indent_filter::push(out,2); 
out.push(std::cout); 

并像这样使用它:

out << "Hello Filter!\n" 
    << indent_in 
    << "this is\n" 
    << "indented\n" 
    << indent_out 
    << "until here\n" 
    ; 

这将导致输出:

Hello Filter! 
  this is 
  indented 
until here 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-17
    • 2015-08-10
    • 2011-01-27
    • 2016-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多