Discussion:
redirect output to /dev/null ?
(too old to reply)
Rui Maciel
2009-01-06 14:11:47 UTC
Permalink
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
2009-01-07 04:07:19 UTC
Permalink
Post by Rui Maciel
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
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Ulrich Eckhardt
2009-01-07 04:06:46 UTC
Permalink
Post by Rui Maciel
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
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
litb
2009-01-08 01:26:58 UTC
Permalink
Post by Ulrich Eckhardt
Post by Rui Maciel
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
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)
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
litb
2009-01-07 04:08:07 UTC
Permalink
Post by Rui Maciel
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!
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
A***@gmail.com
2009-01-07 04:06:24 UTC
Permalink
Post by Rui Maciel
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
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jyoti Sharma
2009-01-07 04:22:32 UTC
Permalink
Post by Rui Maciel
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
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Loading...