chaining c++ streams

穿精又带淫゛_ 提交于 2019-12-06 08:38:25

问题


I was thinking of "chaining" a couple of c++ iostreams toghether to filter input twice. I'm using gzstreams to read zlib compressed files and I was thinking of coding a stream that reads from a stream and performs encoding conversions. Perhaps by passing an opened stream as constructor parameter... How do you think this could be best accomplished?


回答1:


I haven't used this but boost's filtering_stream may help.

As an example I found a mailing list post with indent.hpp, which implements an output filter that indents outputs:

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 


来源:https://stackoverflow.com/questions/818947/chaining-c-streams

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!