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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bo Peng

    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
  • Jonathan Turkanis

    #2
    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::stre am_facade<null_ sink> null_ostream;

    Jonathan


    Comment

    • Siemel Naran

      #3
      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::fa ilbit), 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::fa ilbit);
      o << f(3); // program evaluates f(3) even though it doesn't have to



      Comment

      • JKop

        #4
        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

        Comment

        • Richard Herring

          #5
          Re: Is there a null ostream (like /dev/null) in cpp?

          In message <SIMJc.4687$Z14 .5890@news.indi go.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(cou t); // and this is going to work how, exactly?


          --
          Richard Herring

          Comment

          • JKop

            #6
            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

            Comment

            • Rolf Magnus

              #7
              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";

              Comment

              • JKop

                #8
                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

                Comment

                • Richard Herring

                  #9
                  Re: Is there a null ostream (like /dev/null) in cpp?

                  In message <43PJc.4701$Z14 .5836@news.indi go.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

                  Comment

                  • Rolf Magnus

                    #10
                    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:1 0: 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?

                    Comment

                    • Rolf Magnus

                      #11
                      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.


                      Comment

                      • Jonathan Turkanis

                        #12
                        Re: Is there a null ostream (like /dev/null) in cpp?


                        "Jonathan Turkanis" <technews@kanga roologic.com> wrote in message
                        news:2lp039Fevo 6qU1@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_ty pe c)
                        {
                        return traits_type::no t_eof(c);
                        }

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

                        Jonathan


                        Comment

                        • Bo Peng

                          #13
                          Re: Is there a null ostream (like /dev/null) in cpp?

                          Siemel Naran wrote:[color=blue]
                          >
                          > You can try o.clear(ios::fa ilbit), and then any statement o << 3 will not do
                          > anything.
                          >
                          > o.clear(ios::fa ilbit);
                          > 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

                          Comment

                          • Siemel Naran

                            #14
                            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::fa ilbit), and then any statement o << 3 will[/color][/color]
                            not do[color=blue][color=green]
                            > > anything.
                            > >
                            > > o.clear(ios::fa ilbit);
                            > > 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::fa ilbit)?
                            [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.


                            Comment

                            Working...