cpp中是否有空ostream(如/ dev / null)? [英] Is there a null ostream (like /dev/null) in cpp?

查看:169
本文介绍了cpp中是否有空ostream(如/ dev / null)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的清单,


我需要添加禁用输出特征是一堆对象输出

到ostream(或ofstream)。执行此操作的最不具侵入性的方法是传递

ofstream(" / dev / null")但这会使我的程序不那么便携。

有一个用cpp构建的null ostream吗?是否易于实施?


谢谢。

Bo

解决方案



Bo Peng <沸点*** @ rice.edu>在消息中写道

news:cd ********** @ joe.rice.edu ...

亲爱的名单,
输出到ostream(或ofstream)的一堆对象的特征。执行此操作的最少侵入性方法是传递它们的流(/ dev / null)但这使得我的程序不那么便携。
cpp中是否有一个null ostream?


No.

实施一个是否容易?




是的。从std :: streambuf派生一个类并覆盖受保护的

虚函数溢出,如下所示


int overflow(int c){return c;然后你可以使用标准的istream并使用rdbuf将它的流缓冲区设置为streambuf类的

实例。或者你可以定义你的
自己派生的ostream类,它会自动使用你的streambuf类的一个实例




BTW, Daryle Walker已经提交了一个null_stream类,以便包含在

Boost中,该版本将很快进行审核。 (参见
http://groups.yahoo。 com / group / boost / files / more_io_4.zip 。(你必须

注册boost开发者列表才能访问它:
http://www.boost.org/more/mailing_lists.htm#main 。)另外,我已经写了一张Iostreams图书馆的


http://home.comcast.net/~jturkanis/iostreams/ )允许null

ostream定义如下:


struct null_sink:boost :: io :: sink {

void write(const char *,std :: streamsize){}

} ;


typedef boost :: io :: stream_facade< null_sink> null_ostream;


Jonathan


" Bo Peng" bp *** @ rice.edu>在留言中写道

news:cd ********** @ joe.rice.edu ...

我需要添加禁用输出对一组输出到ostream(或ofstream)的对象的特征。执行此操作的最少侵入性方法是传递它们的流(/ dev / null)但这使得我的程序不那么便携。
是否有一个用cpp构建的null ostream?是否容易实现?




您可以尝试o.clear(ios :: failbit),然后任何语句o<< 3不会做任何事情




如果有人调用o.rdbuf()和
你也可以将rdbuf()设置为NULL
直接写入缓冲区。我不确定标准是否允许你使用
来调用o.rdbuf(NULL)。


缺点:

o.clear(ios :: failbit);

o<< F(3); //程序评估f(3)即使它没有必要


Bo Peng发布:

亲爱的清单,

我需要添加禁用输出输出到ostream(或ofstream)的一堆
对象的特征。执行
的最少侵入性的方法是传递它们ofstream(/ dev / null)但这使得我的程序减少了
便携式。在cpp中是否有一个null ostream?是否很容易
实现一个?
谢谢。
Bo



int cout;

-JKop


Dear list,

I need to add "disable output" feature to a bunch of objects that output
to ostream (or ofstream). The least intrusive way to do this is pass
them ofstream("/dev/null") but this makes my program less portable. Is
there a null ostream built in cpp? Is it easy to implement one?

Thanks.
Bo

解决方案


"Bo Peng" <bp***@rice.edu> wrote in message
news:cd**********@joe.rice.edu...

Dear list,

I need to add "disable output" feature to a bunch of objects that output to ostream (or ofstream). The least intrusive way to do this is pass
them ofstream("/dev/null") but this makes my program less portable. Is there a null ostream built in cpp?
No.
Is it easy to implement one?



Yes. Derive a class from std::streambuf and override the protected
virtual function overflow as follows

int overflow(int c) { return c; }

Then you can use a standard istream and set its stream buffer to an
instance of your streambuf class using rdbuf. Or you can define your
own derived ostream class which automatically uses an an instance of
your streambuf class.

BTW, Daryle Walker has submitted a null_stream class for inclusion in
Boost, which will be reviewed soon. (See
http://groups.yahoo.com/group/boost/files/more_io_4.zip. (You have to
sign up for the boost developers list to access this:
http://www.boost.org/more/mailing_lists.htm#main.) Also, I have
written an Iostreams Library
(http://home.comcast.net/~jturkanis/iostreams/) which allows a null
ostream to be defined as follows:

struct null_sink : boost::io::sink {
void write(const char*, std::streamsize) { }
};

typedef boost::io::stream_facade<null_sink> null_ostream;

Jonathan


"Bo Peng" <bp***@rice.edu> wrote in message
news:cd**********@joe.rice.edu...

I need to add "disable output" feature to a bunch of objects that output
to ostream (or ofstream). The least intrusive way to do this is pass
them ofstream("/dev/null") but this makes my program less portable. Is
there a null ostream built in cpp? Is it easy to implement one?



You can try o.clear(ios::failbit), and then any statement o << 3 will not do
anything.

You can also set the rdbuf() to NULL in case someone calls o.rdbuf() and
writes to the buffer directly. I''m not sure if the standard allows you to
call o.rdbuf(NULL) though.

Disadvantage:

o.clear(ios::failbit);
o << f(3); // program evaluates f(3) even though it doesn''t have to


Bo Peng posted:

Dear list,

I need to add "disable output" feature to a bunch of objects that output to ostream (or ofstream). The least intrusive way to do this is pass them ofstream("/dev/null") but this makes my program less portable. Is there a null ostream built in cpp? Is it easy to implement one?
Thanks.
Bo


int cout;
-JKop


这篇关于cpp中是否有空ostream(如/ dev / null)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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