Overriding std::cout or std::cerr

Discussion in 'C++' started by Jim Langston, Aug 14, 2006.

  1. Jim Langston

    Jim Langston Guest

    I have a computer game that has no console. If I want to output something I
    push a string to the back of a std::vector<std::string> and that gets output
    and maintained.

    All is well and good, but what I would like to do is to be able to use
    ostream's power for this output. I could create my own ostream and check it
    every cycle or something, but then I would have to pass this ostream or make
    it global.

    What I would like to do is have something like:

    std::cout << "Player " << ThisPlayer.Name() << " logged in at: " << time()
    << "\n";

    and have that get redirected, somehow, to my vector.

    If no one has an easy way to do this I'll have to go with the global ostream
    approach and pull everything out of it every cycle.
    Jim Langston, Aug 14, 2006
    #1
    1. Advertising

  2. Jim Langston

    Rolf Magnus Guest

    Jim Langston wrote:

    > I have a computer game that has no console. If I want to output something
    > I push a string to the back of a std::vector<std::string> and that gets
    > output and maintained.
    >
    > All is well and good, but what I would like to do is to be able to use
    > ostream's power for this output. I could create my own ostream and check
    > it every cycle or something, but then I would have to pass this ostream or
    > make it global.
    >
    > What I would like to do is have something like:
    >
    > std::cout << "Player " << ThisPlayer.Name() << " logged in at: " << time()
    > << "\n";
    >
    > and have that get redirected, somehow, to my vector.


    Write a streambuf that appends to the vector, then replace cout's one with
    that.
    Rolf Magnus, Aug 14, 2006
    #2
    1. Advertising

  3. Jim Langston schrieb:
    > I have a computer game that has no console. If I want to output something I
    > push a string to the back of a std::vector<std::string> and that gets output
    > and maintained.
    >
    > All is well and good, but what I would like to do is to be able to use
    > ostream's power for this output. I could create my own ostream and check it
    > every cycle or something, but then I would have to pass this ostream or make
    > it global.
    >
    > What I would like to do is have something like:
    >
    > std::cout << "Player " << ThisPlayer.Name() << " logged in at: " << time()
    > << "\n";
    >
    > and have that get redirected, somehow, to my vector.
    >
    > If no one has an easy way to do this I'll have to go with the global ostream
    > approach and pull everything out of it every cycle.


    I found a nice solution here:

    http://groups.google.com/group/borl...0b0/dfc46e2bfaaa6cbc?lnk=st&#dfc46e2bfaaa6cbc

    ---------
    ostringstream output;

    // set cout's streambuf to be the ostringstream's streambuf
    cout.rdbuf(output.rdbuf());
    ---------

    --
    Thomas
    Thomas J. Gritzan, Aug 14, 2006
    #3
  4. Jim Langston

    Jim Langston Guest

    "Thomas J. Gritzan" <> wrote in message
    news:ebpv1o$g7m$...
    > Jim Langston schrieb:
    >> I have a computer game that has no console. If I want to output
    >> something I
    >> push a string to the back of a std::vector<std::string> and that gets
    >> output
    >> and maintained.
    >>
    >> All is well and good, but what I would like to do is to be able to use
    >> ostream's power for this output. I could create my own ostream and check
    >> it
    >> every cycle or something, but then I would have to pass this ostream or
    >> make
    >> it global.
    >>
    >> What I would like to do is have something like:
    >>
    >> std::cout << "Player " << ThisPlayer.Name() << " logged in at: " <<
    >> time()
    >> << "\n";
    >>
    >> and have that get redirected, somehow, to my vector.
    >>
    >> If no one has an easy way to do this I'll have to go with the global
    >> ostream
    >> approach and pull everything out of it every cycle.

    >
    > I found a nice solution here:
    >
    > http://groups.google.com/group/borl...0b0/dfc46e2bfaaa6cbc?lnk=st&#dfc46e2bfaaa6cbc
    >
    > ---------
    > ostringstream output;
    >
    > // set cout's streambuf to be the ostringstream's streambuf
    > cout.rdbuf(output.rdbuf());
    > ---------
    >
    > --
    > Thomas


    Thanks. That's exactly the type of thing I was looking for and will work
    perfectly.
    Jim Langston, Aug 15, 2006
    #4
    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.
Similar Threads
  1. Trevor
    Replies:
    2
    Views:
    507
  2. Mike Wahler

    cout & cerr

    Mike Wahler, May 24, 2004, in forum: C++
    Replies:
    1
    Views:
    473
    regisser
    May 24, 2004
  3. Rick N. Backer

    cout, cerr and I/O redirection

    Rick N. Backer, Jul 12, 2005, in forum: C++
    Replies:
    2
    Views:
    331
    Ken Wilson
    Jul 15, 2005
  4. saurabh29789

    Sending std::cout to std::cout !!

    saurabh29789, Jun 11, 2009, in forum: C++
    Replies:
    1
    Views:
    282
    Harald Finster
    Jun 11, 2009
  5. saurabh29789

    Sending std::cout to std::cout

    saurabh29789, Jun 11, 2009, in forum: C++
    Replies:
    2
    Views:
    333
    Rolf Magnus
    Jun 12, 2009
Loading...

Share This Page