473,503 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6643
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
7743
by: Trevor | last post by:
Hello, Please bear with me, I am trying to learn C++. I am implementing some error/debug functions which format a message and output it to a C++ stream. I would like to design it using one low...
8
6715
by: Massimiliano Alberti | last post by:
Can I specialize a template function in a subclass without overriding it? (the main template function is defined in a base class). Now I'm doing something like that: (in base class)...
3
1921
by: GGG | last post by:
I have a situation where at tool is passing me a large array of strings that I need to process in a particular type of data. Each item in the array gets to me as a pair of std::strings, basically,...
8
18642
by: Dylan | last post by:
What advantages does std::cerr offer over std::cout? (I'm trying to understand why it exists) thanks
2
4682
by: Rick N. Backer | last post by:
Hello folks, Novice type question here. I'm working with a very simple app here that takes a series of strings from the console. Once the end user has finished inputting the strings they are...
5
4616
by: Cliff Martin | last post by:
Hi, I am writing a simple filter type program that can take input from file or stdin, and output to a file or stdout. It would make life much easier if I could assign cin to a ifstream object...
2
2066
by: arnuld | last post by:
I have 2 programs. 1st PROGRAM: copies an input file to standard output but it does so at the expense of deleting all white-space (space, tabs, newlinies etc). 2nd PROGRAM: copies the input...
5
6500
by: Miles | last post by:
Hello all, When I subclass std::exception and throw an instance of the subclass, when I call the what() method of the caught exception, it does not call the overridden method. I'm under the...
11
6655
by: Adrian | last post by:
Is it possible to save a copy of cout the same way you can save a copy of stdout in C (I know C version is portable, but unix portable is good enough for me). I have to use a library which...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7353
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7011
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4689
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
747
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
401
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.