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

Discussion in 'C++' started by Bo Peng, Jul 16, 2004.

  1. Bo Peng

    Bo Peng Guest

    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
     
    Bo Peng, Jul 16, 2004
    #1
    1. Advertising

  2. "Bo Peng" <> wrote in message
    news:cd7flb$4tn$...
    > 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
     
    Jonathan Turkanis, Jul 16, 2004
    #2
    1. Advertising

  3. Bo Peng

    Siemel Naran Guest

    "Bo Peng" <> wrote in message
    news:cd7flb$4tn$...

    > 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
     
    Siemel Naran, Jul 16, 2004
    #3
  4. Bo Peng

    JKop Guest

    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
     
    JKop, Jul 16, 2004
    #4
  5. In message <SIMJc.4687$>, JKop <>
    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::eek:stream &);
    };

    some_class x;
    int cout;
    x.output_me(cout); // and this is going to work how, exactly?


    --
    Richard Herring
     
    Richard Herring, Jul 16, 2004
    #5
  6. Bo Peng

    JKop Guest

    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
     
    JKop, Jul 16, 2004
    #6
  7. Bo Peng

    Rolf Magnus Guest

    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";
     
    Rolf Magnus, Jul 16, 2004
    #7
  8. Bo Peng

    JKop Guest

    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
     
    JKop, Jul 16, 2004
    #8
  9. In message <43PJc.4701$>, JKop <>
    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
     
    Richard Herring, Jul 16, 2004
    #9
  10. Bo Peng

    Rolf Magnus Guest

    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?
     
    Rolf Magnus, Jul 16, 2004
    #10
  11. Bo Peng

    Rolf Magnus Guest

    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.
     
    Rolf Magnus, Jul 16, 2004
    #11
  12. "Jonathan Turkanis" <> wrote in message
    news:...

    > 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
     
    Jonathan Turkanis, Jul 16, 2004
    #12
  13. Bo Peng

    Bo Peng Guest

    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
     
    Bo Peng, Jul 16, 2004
    #13
  14. Bo Peng

    Siemel Naran Guest

    "Bo Peng" <> wrote in message
    news:cd95po$eub$...
    > 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.
     
    Siemel Naran, Jul 18, 2004
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page