473,414 Members | 1,709 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,414 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 10757

"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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.