For Programmers: Free Programming Magazines  


Home > Archive > VC STL > March 2005 > Standard output into a file or stream









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Standard output into a file or stream
Carlos Galavis

2005-03-25, 4:01 pm

Hi,

I need to store the output of a console application into a file, but I also
need the output to continue to go to the console, so I can't just redirect,
I need something like a callback, or some way of getting notified when
something is added to the standard output stream so that I can appended it
to the file.

Does anyone have an idea on how to accomplishe this?

Thanks,

Carlos


Igor Tandetnik

2005-03-25, 4:01 pm

"Carlos Galavis" <cc@cc.com> wrote in message
news:O7Pg5hVMFHA.2576@TK2MSFTNGP10.phx.gbl
> I need to store the output of a console application into a file, but
> I also need the output to continue to go to the console, so I can't
> just redirect, I need something like a callback, or some way of
> getting notified when something is added to the standard output
> stream so that I can appended it to the file.
>
> Does anyone have an idea on how to accomplishe this?


In Linux (and probably other Unix-like systems) there is a command-line
utility that takes the file name on the command line, reads the standard
input and dumps it both to the standard output and to this file (the
name of the utility escapes me at the moment). It should be easy to
write something like this, then chain it with your console app.

For another solution, see

http://msdn.microsoft.com/library/e..._and_output.asp

--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


adebaene@club-internet.fr

2005-03-25, 4:01 pm


Carlos Galavis wrote:
> Hi,
>
> I need to store the output of a console application into a file, but

I also
> need the output to continue to go to the console, so I can't just

redirect,
> I need something like a callback, or some way of getting notified

when
> something is added to the standard output stream so that I can

appended it
> to the file.
>
> Does anyone have an idea on how to accomplishe this?


If you want to do the STL way, you need to define a multiplexing
streambuf (let's call it "TeeStreamBuf") that takes 2 (or more)
streambufs references in constructor and forwards all received data to
all connected streambufs.

Next, you build a TeeStreamBuf that is connected to cout::rdbuf and to
a filebuf, and finally build a ostream that uses your TeeStreamBuf.

Arnaud
MVP - VC

Carlos Galavis

2005-03-25, 4:01 pm

Thanks Arnaud,

This worked great, but while doing it, I discover that the problem is a bit
more complicated than what I originally thought.

The code that needs to redirect the console (or mirror it) to another stream
is in a DLL that is dynamically loaded by the host application (it is a
plug-in architecture), but it seams the host is not using the standard
output stream "cout" to write to the console, I miss everything that the
host application is writing to the console, but I do get whatever I write
from the DLL as long as I use "cout". The host application provides a
callback interface that enables writing messages to the console, if I use
this interface, the output is not picketed up by my stream either.

Any clues?

Thanks again,

Carlos


>
> If you want to do the STL way, you need to define a multiplexing
> streambuf (let's call it "TeeStreamBuf") that takes 2 (or more)
> streambufs references in constructor and forwards all received data to
> all connected streambufs.
>
> Next, you build a TeeStreamBuf that is connected to cout::rdbuf and to
> a filebuf, and finally build a ostream that uses your TeeStreamBuf.
>
> Arnaud
> MVP - VC
>



Arnaud Debaene

2005-03-25, 4:01 pm

Carlos Galavis wrote:
> Thanks Arnaud,
>
> This worked great, but while doing it, I discover that the problem is
> a bit more complicated than what I originally thought.
>
> The code that needs to redirect the console (or mirror it) to another
> stream is in a DLL that is dynamically loaded by the host application
> (it is a plug-in architecture), but it seams the host is not using
> the standard output stream "cout" to write to the console, I miss
> everything that the host application is writing to the console, but I
> do get whatever I write from the DLL as long as I use "cout". The
> host application provides a callback interface that enables writing
> messages to the console, if I use this interface, the output is not
> picketed up by my stream either.
> Any clues?


2 ideas :
- Either your app, either your DLL (either both) are not linked against the
same DLL version of the CRT. In such a case, std::cout in the DLL and
std::cout in the EXE do not refer to the same object. Make sure both EXE and
DLL are compiled both either with /MD either with /MDd.
- The EXE is not using cout at all for outputting to the console!

In both cases, if you can't access the exe code source, I fear you're out of
luck : you'll need to hook at the Win32 console API level : See
GetStdHandle/SetStdHandle.

Arnaud
MVP - VC


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com