Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Bo Peng
Guest
 
Posts: n/a
#1: Jul 22 '05
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

Jonathan Turkanis
Guest
 
Posts: n/a
#2: Jul 22 '05

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



"Bo Peng" <bpeng@rice.edu> wrote in message
news:cd7flb$4tn$1@joe.rice.edu...[color=blue]
> Dear list,
>
> I need to add "disable output" feature to a bunch of objects that[/color]
output[color=blue]
> to ostream (or ofstream). The least intrusive way to do this is pass
> them ofstream("/dev/null") but this makes my program less portable.[/color]
Is[color=blue]
> there a null ostream built in cpp?[/color]

No.
[color=blue]
>Is it easy to implement one?[/color]

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


Siemel Naran
Guest
 
Posts: n/a
#3: Jul 22 '05

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


"Bo Peng" <bpeng@rice.edu> wrote in message
news:cd7flb$4tn$1@joe.rice.edu...
[color=blue]
> 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?[/color]

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



JKop
Guest
 
Posts: n/a
#4: Jul 22 '05

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


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


int cout;


-JKop
Richard Herring
Guest
 
Posts: n/a
#5: Jul 22 '05

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


In message <SIMJc.4687$Z14.5890@news.indigo.ie>, JKop <NULL@NULL.NULL>
writes[color=blue]
>Bo Peng posted:
>[color=green]
>> Dear list,
>>
>> I need to add "disable output" feature to a bunch of[/color]
>objects that output[color=green]
>> to ostream (or ofstream). The least intrusive way to do[/color]
>this is pass[color=green]
>> them ofstream("/dev/null") but this makes my program less[/color]
>portable. Is[color=green]
>> there a null ostream built in cpp? Is it easy to[/color]
>implement one?[color=green]
>>[/color]
>
>int cout;[/color]

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
JKop
Guest
 
Posts: n/a
#6: Jul 22 '05

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



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
Rolf Magnus
Guest
 
Posts: n/a
#7: Jul 22 '05

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


JKop wrote:
[color=blue]
> Bo Peng posted:
>[color=green]
>> Dear list,
>>
>> I need to add "disable output" feature to a bunch of[/color]
> objects that output[color=green]
>> to ostream (or ofstream). The least intrusive way to do[/color]
> this is pass[color=green]
>> them ofstream("/dev/null") but this makes my program less[/color]
> portable. Is[color=green]
>> there a null ostream built in cpp? Is it easy to[/color]
> implement one?[color=green]
>>
>> Thanks.
>> Bo[/color]
>
>
> int cout;[/color]

cout << "Hello world\n";

JKop
Guest
 
Posts: n/a
#8: Jul 22 '05

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


Rolf Magnus posted:

[color=blue]
> cout << "Hello world\n";
>
>[/color]

Allow me to sabotage your prog:


#include <iostream>

using std::cout;


int main()
{
int cout;

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



-JKop
Richard Herring
Guest
 
Posts: n/a
#9: Jul 22 '05

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


In message <43PJc.4701$Z14.5836@news.indigo.ie>, JKop <NULL@NULL.NULL>
writes[color=blue]
>Rolf Magnus posted:
>
>[color=green]
>> cout << "Hello world\n";
>>
>>[/color]
>
>Allow me to sabotage your prog:
>
>
>#include <iostream>
>
>using std::cout;
>
>
>int main()
>{
> int cout;
>
> cout << "Hello World!!\n";[/color]

E2087 Illegal use of pointer.[color=blue]
>}
>
>[/color]

--
Richard Herring
Rolf Magnus
Guest
 
Posts: n/a
#10: Jul 22 '05

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


JKop wrote:
[color=blue]
> Rolf Magnus posted:
>
>[color=green]
>> cout << "Hello world\n";
>>
>>[/color]
>
> Allow me to sabotage your prog:
>
>
> #include <iostream>
>
> using std::cout;
>
>
> int main()
> {
> int cout;
>
> cout << "Hello World!!\n";
> }[/color]

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?

Rolf Magnus
Guest
 
Posts: n/a
#11: Jul 22 '05

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


JKop wrote:
[color=blue]
> 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.[/color]

Doesn't look immature to me, but I can't find anything funny in that
either.


Jonathan Turkanis
Guest
 
Posts: n/a
#12: Jul 22 '05

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



"Jonathan Turkanis" <technews@kangaroologic.com> wrote in message
news:2lp039Fevo6qU1@uni-berlin.de...
[color=blue]
> Yes. Derive a class from std::streambuf and override the protected
> virtual function overflow as follows
>
> int overflow(int c) { return c; }[/color]

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


Bo Peng
Guest
 
Posts: n/a
#13: Jul 22 '05

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


Siemel Naran wrote:[color=blue]
>
> 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[/color]

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
Siemel Naran
Guest
 
Posts: n/a
#14: Jul 22 '05

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


"Bo Peng" <bpeng@rice.edu> wrote in message
news:cd95po$eub$1@joe.rice.edu...[color=blue]
> Siemel Naran wrote:[/color]
[color=blue][color=green]
> > You can try o.clear(ios::failbit), and then any statement o << 3 will[/color][/color]
not do[color=blue][color=green]
> > anything.
> >
> > o.clear(ios::failbit);
> > o << f(3); // program evaluates f(3) even though it doesn't have to[/color]
>
> 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.[/color]

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)?
[color=blue]
> 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.[/color]

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.


Closed Thread


Similar C / C++ bytes