Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

redirect output to /dev/null ?

1,075 views
Skip to first unread message

Rui Maciel

unread,
Jan 6, 2009, 9:11:47 AM1/6/09
to
Is there any standard output object like cout or cerr that acts like
/dev/null?


Thanks in advance,
Rui Maciel

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

JC

unread,
Jan 6, 2009, 11:07:19 PM1/6/09
to
On Jan 6, 9:11 am, Rui Maciel <rui.mac...@gmail.com> wrote:
> Is there any standard output object like cout or cerr that acts like
> /dev/null?


If std::ofstream("/dev/null") is not sufficient, there are other
solutions (it's trivial to implement a null ifstream / ofstream). Have
a look here:

http://www.velocityreviews.com/forums/showpost.php?p=1501892&postcount=2

The rest of the posts in that thread besides that one are generally
uninformative.

I'm pretty sure the null_stream thing mentioned there never made it
into boost, btw. Searching for it yielded some boost peer reviews and
the ones I read did not look too promising.

Jason

Ulrich Eckhardt

unread,
Jan 6, 2009, 11:06:46 PM1/6/09
to
Rui Maciel wrote:
> Is there any standard output object like cout or cerr that acts like
> /dev/null?

No.

That said, a stream without a streambuffer doesn't actually write its output
anywhere:

// 1 - redirect output
std::cout.rdbuf(0);
std::cout << "look I'm invisible!";

// 2 - write to sinkless stream
std::ostream out(0);
out << "still can't see me?";


Uli

litb

unread,
Jan 6, 2009, 11:08:07 PM1/6/09
to
On 6 Jan., 15:11, Rui Maciel <rui.mac...@gmail.com> wrote:
> Is there any standard output object like cout or cerr that acts like
> /dev/null?
>

There is no such default stream type. You can, however, get that
effect by defining your own null-streambuf that just discards any
characters it is handed over:

template<typename Ch, typename Traits = std::char_traits<Ch> >
struct basic_nullbuf : std::basic_streambuf<Ch, Traits> {
typedef std::basic_streambuf<Ch, Traits> base_type;
typedef typename base_type::int_type int_type;
typedef typename base_type::traits_type traits_type;

virtual int_type overflow(int_type c) {
return traits_type::not_eof(c);
}
};

// convenient typedefs
typedef basic_nullbuf<char> nullbuf;
typedef basic_nullbuf<wchar_t> wnullbuf;

// buffers and streams
nullbuf cnull_obj;
wnullbuf wcnull_obj;
std::ostream cnull(&cnull_obj);
std::wostream wcnull(&wcnull_obj);

Keep the buffers and streams in the same file, to avoid static
initialization order problems. I have seen someone used a not-opened
ofstream to do the same and said that writing to it is a noop, but i
haven't been able to verify that since then. So i would better be on
the safe side writing my own like that. Have fun!

AnonMa...@gmail.com

unread,
Jan 6, 2009, 11:06:24 PM1/6/09
to
> Is there any standard output object like cout or cerr that acts like
> /dev/null?

No. But the boost iostreams library have null sources and sinks which
I think will do the trick.

HTH

Jyoti Sharma

unread,
Jan 6, 2009, 11:22:32 PM1/6/09
to
On Tue, 06 Jan 2009 19:41:47 +0530, Rui Maciel <rui.m...@gmail.com> wrote:

> Is there any standard output object like cout or cerr that acts like
> /dev/null?
>

I am not aware of any such standard object. But you can very well redirect it to /dev/null easily and achieve the same effect--probably that is why it is not there.

Regards,
Jyoti

litb

unread,
Jan 7, 2009, 8:26:58 PM1/7/09
to
On 7 Jan., 05:06, Ulrich Eckhardt <dooms...@knuut.de> wrote:
> Rui Maciel wrote:
> > Is there any standard output object like cout or cerr that acts like
> > /dev/null?
>
> No.
>
> That said, a stream without a streambuffer doesn't actually write its output
> anywhere:
>
I've always wondered where in the Standard it says that. Can you or
another guy tell me the source of that? (Not to say i wouldn't believe
you, i just want to read further about it)
0 new messages