473,748 Members | 10,058 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 6670
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(outp ut.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(outp ut.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
7757
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 level function which accepts a "generic C++ stream" as an argument (cerr, cout, ofstream, etc). I have tried to make the parameter a const ostream&, but when I plug in cerr or cout I get a compiler error. What am I doing wrong? // The low...
8
6740
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) template<class X> void myfunc(X par1) { } (in derived class) template<class X>
3
1936
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, one string represents the data, one string represents the data type. Here's a quick example of what it sort of looks like string data = { "0.0000002342", "12.00234", "42", "5" }; string types = { "EF", "EF", "I", "I" }; //EF is 64 bit float,...
8
18652
by: Dylan | last post by:
What advantages does std::cerr offer over std::cout? (I'm trying to understand why it exists) thanks
2
4700
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 individually processed and the result displayed back to the console. The problem I am having occurs during I/O redirection. Here is where
5
4630
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 and read it like a file and do the same for cout (to an ofstream). Any ideas how I could do this? Or perhaps some comments on how I could redo the code to do what I want? Simplified code follows: #include <iostream>
2
2081
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 as it is to the standard output.
5
6534
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 impression that std::exception declares what() as a virtual method, so I'm at a loss as to why the subclass's what() is not being called. For example, for this program: #include <exception>
11
6745
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 closes stdout on init because its used to mostly in background processes but I need it for an interactive process. Library cant be changed unfortunately. I tried the following but the C++ version doesn't work, I guess because it only shares the...
0
8984
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8823
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9238
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8237
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.