如何创建自己的ostream / streambuf? [英] How do I create my own ostream/streambuf?

查看:503
本文介绍了如何创建自己的ostream / streambuf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了教育目的,我想创建一个ostream和流缓冲区来执行:

For educational purposes I want to create a ostream and stream buffer to do:


  1. 在执行< myVar;

  2. 存储在deque容器中,而不是使用std:cout或写入文件

  3. 记录额外的数据, ;<我做了多少次.write,写入的字节数以及flush()的次数。但我不需要所有的信息。

我尝试重载但失败可怕。我尝试过重写

I tried overloading but failed horribly. I tried overloading write by doing

ostream& write( const char* s, streamsize n ) 



< cpp文件并修改它),但代码保持使用basic_ostream。我通过代码看起来,它看起来像我需要重载xsputn(这不是在这个页面上提到 http:// www。 cplusplus.com/reference/iostream/ostream ),但是我还需要什么呢?

in my basic_stringstream2 class (I copied paste basic_stringstream into my cpp file and modified it) but the code kept using basic_ostream. I looked through code and it looks like I need to overload xsputn (which isn't mention on this page http://www.cplusplus.com/reference/iostream/ostream ) but what else do I need to overload? and how do I construct my class (what does it need to inherit, etc)?

推荐答案

规范化的方法包括定义你的自己的streambuf。
您应该看看:

The canonical approach consists in defining your own streambuf. You should have a look at:

  • Angelika LAnger's articles on IOStreams derivation
  • James Kanze's articles on filtering streambufs
  • boost.iostream for examples of application

这篇关于如何创建自己的ostream / streambuf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆