473,326 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Is there a null ostream (like /dev/null) in cpp?

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
Jul 22 '05 #1
13 10710

"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
Jul 22 '05 #2
"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

Jul 22 '05 #3
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
Jul 22 '05 #4
In message <SI*****************@news.indigo.ie>, JKop <NU**@NULL.NULL>
writes
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?


int cout;


Yeah, right, that's really helpful.

He never mentions cout; he wants to pass an ostream to an object.

class some_class {
public:
void output_me(std::ostream &);
};

some_class x;
int cout;
x.output_me(cout); // and this is going to work how, exactly?
--
Richard Herring
Jul 22 '05 #5

It was a joke!

Consider code like this:

using std::cout;

int main()
{
cout << "blah";

cout << "jhskfh";
}
If you stick "int cout" at the start of main(), then it'll
hide the now global scope "cout" object.

Maybe I'm immature, but I find that amusing.
-JKop
Jul 22 '05 #6
JKop wrote:
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;


cout << "Hello world\n";

Jul 22 '05 #7
Rolf Magnus posted:

cout << "Hello world\n";


Allow me to sabotage your prog:
#include <iostream>

using std::cout;
int main()
{
int cout;

cout << "Hello World!!\n";
}

-JKop
Jul 22 '05 #8
In message <43*****************@news.indigo.ie>, JKop <NU**@NULL.NULL>
writes
Rolf Magnus posted:

cout << "Hello world\n";
Allow me to sabotage your prog:
#include <iostream>

using std::cout;
int main()
{
int cout;

cout << "Hello World!!\n";


E2087 Illegal use of pointer.}


--
Richard Herring
Jul 22 '05 #9
JKop wrote:
Rolf Magnus posted:

cout << "Hello world\n";


Allow me to sabotage your prog:
#include <iostream>

using std::cout;
int main()
{
int cout;

cout << "Hello World!!\n";
}


g++ says:

fake_cout.cpp: In function `int main()':
fake_cout.cpp:10: error: invalid operands of types `int' and `const
char[15]' to binary `operator<<'

And even if this worked as you might have expected, how would it satisfy
the OP's request for a stream that just throws everything away?

Jul 22 '05 #10
JKop wrote:
It was a joke!

Consider code like this:

using std::cout;

int main()
{
cout << "blah";

cout << "jhskfh";
}
If you stick "int cout" at the start of main(), then it'll
hide the now global scope "cout" object.

Maybe I'm immature, but I find that amusing.


Doesn't look immature to me, but I can't find anything funny in that
either.
Jul 22 '05 #11

"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message
news:2l************@uni-berlin.de...
Yes. Derive a class from std::streambuf and override the protected
virtual function overflow as follows

int overflow(int c) { return c; }


Strictly, this is wrong if c == EOF. For an arbitrary character and
traits type, the implementation would be

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

For the case discussed, int overflow(int c) { return 0; }should
suffice.

Jonathan
Jul 22 '05 #12
Siemel Naran wrote:

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

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


This seems to be an easy solution but in practice I have to create 'o'
in some way. As far as I know, I can not instantiate ostream like
ostream o;
o.clear(.....)
and if I use
ofstream o(...)
I have to use a valid filename which will create an empty file even if I
do not write anything to it.

Of course it is a bad idea to cripple cout or cerr by
cout.clear(ios::failbit).

I guess I will follow Jonathan's method.

Bo Peng
Jul 22 '05 #13
"Bo Peng" <bp***@rice.edu> wrote in message
news:cd**********@joe.rice.edu...
Siemel Naran wrote:
You can try o.clear(ios::failbit), and then any statement o << 3 will not do anything.

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


This seems to be an easy solution but in practice I have to create 'o'
in some way. As far as I know, I can not instantiate ostream like
ostream o;
o.clear(.....)
and if I use
ofstream o(...)
I have to use a valid filename which will create an empty file even if I
do not write anything to it.


If you use an invalid filename, the stream will have the failbit and
possibly the badbit set. Why is it important to instantiate o with the
failbit set, and what's wrong with ostream o followed by
o.clear(ios::failbit)?
Of course it is a bad idea to cripple cout or cerr by
cout.clear(ios::failbit).

I guess I will follow Jonathan's method.


Yes, it is cleaner. But it's also not a good idea to replace the underlying
streambuf of cout, cerr. If you do, remember to restore the original
streambuf, else the program may crash at program termination.
Jul 22 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Victor Irzak | last post by:
Hello, I have an ABC. it supports: ostream & operator << I also have a derived class that supports this operator. How can I call operator << of the base class for derived object??? Is it...
7
by: Stewart Gordon | last post by:
Using g++ 3.1, Mac OS X. I'm doing something trying to involve a null output stream, i.e. a stream that just discards what goes into it. The idea is that it gives a simple way of producing...
6
by: nish.parikh | last post by:
Hi, I am using std::cout to print a char pointer that is NULL. Subsequent calls to std::cout dont print anything. Is this the expected behavior? example: #include <iostream> int main( int...
13
by: Angel Tsankov | last post by:
How do I define a null ostream that inherits publicly std::ostream and ignores anything that would otherwise be output?
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
4
by: isanbard | last post by:
Hi all, I'd like to do something like this: #include <ostream> std::ostream cnull(0); #ifdef NDEBUG #define DOUT cnull
4
by: toton | last post by:
Hi, I want to have a log stream, where I can put certain logs. The log can be console or file or any other ui widget. This part I had already done with. However I want log stream (std::ostream...
11
by: coomberjones | last post by:
I have a few std::strings that I am using to store raw binary data, each of which may very well include null bytes at any point or points. I want to slap them together into a single string, so I...
3
by: Thomas Lenz | last post by:
The code below should allow to use a comma instead of << with ostreams and include a space between two operands when comma is used. e.g. cout << "hello", "world", endl; should print the line...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.