473,397 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

intercepting output to cout

I want to create an object,
which replaces std::cout
all output will be put out to std::cout, but optional
also copied to a file.

But the file output needs to be manipulated:
every new line has to be preceded by a ';'

Is there an easy way to do so?
Or do I have to write wrappers for all kind of
<< operators?

And which functions to overload to scan for '\n'
likje ostr.put(...), any other (ie. are operator<<
using put(...) (always)?)

thanks,
marc

Jul 22 '05 #1
5 2610
"Marc Schellens" <m_*********@hotmail.com> wrote in message
news:ck***********@news.riken.go.jp...
I want to create an object,
which replaces std::cout
all output will be put out to std::cout, but optional
also copied to a file.

But the file output needs to be manipulated:
every new line has to be preceded by a ';'

Is there an easy way to do so?
Or do I have to write wrappers for all kind of
<< operators?

And which functions to overload to scan for '\n'
likje ostr.put(...), any other (ie. are operator<<
using put(...) (always)?)

Look for "redirect cout" ...
What you need to do is replace the streambuf associated
with std::cout:

int main(void)
{
std:ostream * saveStream = cout.rdbuf();
MyStreamBuffer newBuf(.......);
cout.rdbuf(&newBuf);
try {
... the resto of te program ...

} catch(...) { error reporting... }
cout.rdbuf(saveStream); // restore cout to original state
}

To have output to both the console and a file, you should be
able to find a 'tee' stream buffer (I do not know a specific
implementation, but there are a few floating around on the web):
http://www.google.com/search?&q=Tee%20streambuf
hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Jul 22 '05 #2
Ivan Vecerina wrote:

Look for "redirect cout" ...
What you need to do is replace the streambuf associated
with std::cout:


that was exactly what I needed. Thanks,
marc

Jul 22 '05 #3

"Ivan Vecerina" <pl*****************@ivan.vecerina.com> wrote in message
news:ck**********@newshispeed.ch...
"Marc Schellens" <m_*********@hotmail.com> wrote in message
news:ck***********@news.riken.go.jp...
int main(void)
{
std:ostream * saveStream = cout.rdbuf();
MyStreamBuffer newBuf(.......);
cout.rdbuf(&newBuf);
try {
... the resto of te program ...

} catch(...) { error reporting... }
cout.rdbuf(saveStream); // restore cout to original state
}


What if there is no exception? How does cout get restored in that case?
I'd think you'd want to use a constructor/destructor pair for the rdbuf
calls, wouldn't you? (think "RAII")

-Howard
Jul 22 '05 #4
"Howard" <al*****@hotmail.com> wrote in message
news:lB*********************@bgtnsc04-news.ops.worldnet.att.net...

"Ivan Vecerina" <pl*****************@ivan.vecerina.com> wrote in message
news:ck**********@newshispeed.ch...
"Marc Schellens" <m_*********@hotmail.com> wrote in message
news:ck***********@news.riken.go.jp...
int main(void)
{
std:ostream * saveStream = cout.rdbuf();
MyStreamBuffer newBuf(.......);
cout.rdbuf(&newBuf);
try {
... the resto of te program ...

} catch(...) { error reporting... }
cout.rdbuf(saveStream); // restore cout to original state
}


What if there is no exception? How does cout get restored in that case?
I'd think you'd want to use a constructor/destructor pair for the rdbuf
calls, wouldn't you? (think "RAII")

I would definitely use RAII -- except I wanted this post to be concise
and there is no RAII class performing the above in the standard library.
This said:
- int main() above does catch(...) { /*code*/ } to always restore the buf
- anyway it is not a good idea to let exceptions propagate out of main()

Cheers -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form
Brainbench MVP for C++ <> http://www.brainbench.com


Jul 22 '05 #5

"Ivan Vecerina" <pl*****************@ivan.vecerina.com> wrote in message
news:ck**********@newshispeed.ch...
"Howard" <al*****@hotmail.com> wrote in message
news:lB*********************@bgtnsc04-news.ops.worldnet.att.net...

"Ivan Vecerina" <pl*****************@ivan.vecerina.com> wrote in message
news:ck**********@newshispeed.ch...
> "Marc Schellens" <m_*********@hotmail.com> wrote in message
> news:ck***********@news.riken.go.jp...
> int main(void)
> {
> std:ostream * saveStream = cout.rdbuf();
> MyStreamBuffer newBuf(.......);
> cout.rdbuf(&newBuf);
> try {
> ... the resto of te program ...
>
> } catch(...) { error reporting... }
> cout.rdbuf(saveStream); // restore cout to original state
> }
>


What if there is no exception? How does cout get restored in that case?
I'd think you'd want to use a constructor/destructor pair for the rdbuf
calls, wouldn't you? (think "RAII")

I would definitely use RAII -- except I wanted this post to be concise
and there is no RAII class performing the above in the standard library.
This said:
- int main() above does catch(...) { /*code*/ } to always restore the buf
- anyway it is not a good idea to let exceptions propagate out of main()


I misread your code. The way you put "{ error reporting... }" on the same
line as catch, I mistook that for a comment. (My old Pascal days coming
back!) And so I then mistook the buffer restore call as being inside the
catch clause. My bad!

But that's one reason I always go down to the next line before starting the
"dependant clause" after a try/catch/if/else, etc. Arranging it like this
always confuses my little mind. :-)

-Howard

Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: rossum | last post by:
I have been looking at exceptions as I need to get better at using them. I came across an interesting effect, demonstrated below. When I ctrl-Z the input to throw an ios_base::failure, the...
6
by: radnoraj | last post by:
Hi, I am sucessfull in redirecting console output to a file. but in this case nothing is displayed on the console, cout output is written to file without display. how do write the output to...
11
by: Roger Leigh | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 The program listed below demonstrates the use of wcsftime() and std::time_put<wchar_t> which is a C++ wrapper around it. (I know this isn't C; but...
6
by: Valery Polyakov | last post by:
I am drawing lines with a semi-transparent color (alpha=100). When these lines intercept, the color becomes more intense. Is there a way to avoid this, so that I would get the same intensity of...
2
by: Nick | last post by:
Hi, Is it possible to pass execution to a view (aspx file), let it do its job, and then capture the output? I want to achieve something like this during a HttpRequest: 1. Invoke the...
8
by: Blah | last post by:
Hi, this is a small program to print out three lists of values. One is a simple increment count the others are values from two equations. I've run this without problems before but when I've put in...
19
by: Dancefire | last post by:
Hi, everyone It might be a simple question, but I really don't know the answer. char c = '1'; cout << c; The above code will only output a '1' rather than 0x31; If I use int cast, it can...
2
by: Chavs | last post by:
Hello, This is my first post, hopefully in the right area. I'm quite new to C++ so dont know advanced techniques, but Im trying to get my head around a task weve been given during a lab at uni. ...
0
by: acehreli | last post by:
On Jul 7, 9:56 am, Carsten Fuchs <CarstenFu...@T-Online.dewrote: One option is to leave cout to the others and maintain your own ostream which shares the same output buffer (but not formatting)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
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
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...
0
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...

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.