c++ - 链接 C++ 流

标签 c++ iostream

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

最佳答案

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

例如,我找到了 a mailing list postindent.hpp ,它实现了一个缩进输出的输出过滤器:

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

And use it like so:

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

Which will result in output:

Hello Filter! 
  this is 
  indented 
until here 

关于c++ - 链接 C++ 流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/818947/

相关文章:

c++ - 如何在 C++ 中同时读写 `fstream` 的文件?

c++ - OpenGL 中没有显示任何内容

c++ - 在 Windows 2008 64 位中创建注册表项

c++ - 使用 Win32 API 获取 Windows 屏幕保护程序超时

c++ - 我可以在 "\n"上停止 std::cout 刷新吗?

c++ - 如何解决 C++ 中的这个运行时错误? - "Stack around variable was corrupted"

c++ - 如何将 "Dummy"值替换为 std::cin?

c++ - 正则表达式查找所有用位字段定义的结构

c++ - free() :invalid size, 但没有调用 new() 或 free() - 使用 C++ 的 SQLite3

c++ - 无法从文本文件中读取并存储到字符串中