471,831 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,831 software developers and data experts.

Overriding std::cout or std::cerr

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::stringand 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.
Aug 14 '06 #1
3 6400
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::stringand 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.

Aug 14 '06 #2
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::stringand 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/borla...c46e2bfaaa6cbc

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

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

--
Thomas
Aug 14 '06 #3
"Thomas J. Gritzan" <Ph*************@gmx.dewrote in message
news:eb**********@newsreader2.netcologne.de...
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::stringand 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/borla...c46e2bfaaa6cbc

---------
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.
Aug 15 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Massimiliano Alberti | last post: by
3 posts views Thread by GGG | last post: by
8 posts views Thread by Dylan | last post: by
2 posts views Thread by Rick N. Backer | last post: by
5 posts views Thread by Cliff Martin | last post: by
2 posts views Thread by arnuld | last post: by
5 posts views Thread by Miles | last post: by
11 posts views Thread by Adrian | last post: by
NeoPa
reply views Thread by NeoPa | last post: by
aboka
reply views Thread by aboka | last post: by

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.