Topic: Redirecting cout and stdout to a file


Author: mauricefxxx@ix.netcom.com (Maurice Fox)
Date: 2000/11/20
Raw View
On Sun, 12 Nov 3900 23:16:15, kuehl@ramsen.informatik.uni-konstanz.de
(Dietmar Kuehl) wrote:

> Hi,
> Sai Shankar (Sai.Shankar@blr.spcnl.co.in) wrote:
> : Im writing a C++ program in which I'm interfacing with
> : a C library which printf's its traces in debug mode. I
> : would like to know how to ensure that the printf's as
> : well as the ouputs to cout are put in the same log file
> : from within the program.
>
> 'std::stdout' can be changed to write to some file using 'freopen()'.
> The standard C++ library's 'std::cout' is not [necessarily] affected by
> this operation. This is at least my reading of the standard...
>
> Thus, my approach would be the install of a specialized stream buffer
> into 'std::cout' which simply directs its output to 'std::stdout': You
> simply redirect 'std::cout' to write to 'std::stdout' not to just the
> same default destination and then redirecting 'std::stdout' to some
> file would also redirect 'std::cout'.
> --
> <mailto:dietmar_kuehl@yahoo.de>
> <http://www.fmi.uni-konstanz.de/~kuehl/>
> I am a realistic optimist - that's why I appear to be slightly pessimistic
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
> [ Note that the FAQ URL has changed!  Please update your bookmarks.     ]
>

Look at the overloaded rdbuf() function of the iostream classes.  See
Josuttis, 13.10.2.  You can redirect the stream where you want.

HTH - Maurice

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: kuehl@ramsen.informatik.uni-konstanz.de (Dietmar Kuehl)
Date: 2000/11/12
Raw View
Hi,
Sai Shankar (Sai.Shankar@blr.spcnl.co.in) wrote:
: Im writing a C++ program in which I'm interfacing with
: a C library which printf's its traces in debug mode. I
: would like to know how to ensure that the printf's as
: well as the ouputs to cout are put in the same log file
: from within the program.

'std::stdout' can be changed to write to some file using 'freopen()'.
The standard C++ library's 'std::cout' is not [necessarily] affected by
this operation. This is at least my reading of the standard...

Thus, my approach would be the install of a specialized stream buffer
into 'std::cout' which simply directs its output to 'std::stdout': You
simply redirect 'std::cout' to write to 'std::stdout' not to just the
same default destination and then redirecting 'std::stdout' to some
file would also redirect 'std::cout'.
--
<mailto:dietmar_kuehl@yahoo.de>
<http://www.fmi.uni-konstanz.de/~kuehl/>
I am a realistic optimist - that's why I appear to be slightly pessimistic

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: Sai Shankar <Sai.Shankar@blr.spcnl.co.in>
Date: 2000/11/10
Raw View
Hello,

Im writing a C++ program in which I'm interfacing with
a C library which printf's its traces in debug mode. I
would like to know how to ensure that the printf's as
well as the ouputs to cout are put in the same log file
from within the program.

I know how to (re)set the stream buffer for cout and how to
synchronize the output to stdout and cout using
std::sync_with_stdio. But how can i do both of these at
the same time programmatically? Some attempts given below:

// Attempt #1
// Call sync_with_stdio and synchronize stdout/cout
// Replace the common(?) buffer through cout's rdbuf
// function and try.

#include <stdio.h>
#include <iostream>
#include <string>
#include <sstream>

int main ()
{
  std::ios::sync_with_stdio ();

  std::stringstream ss;

  std::streambuf* coutbuf = std::cout.rdbuf ();

  std::streambuf* stringbuf = ss.rdbuf ();

  std::cout.rdbuf (stringbuf);

  std::cout << "Test output to cout\n";

  printf ("Test output to stdout\n");

  std::cout.rdbuf (coutbuf);

  std::cout << ss.str ();
}

With Borland 5.5 I get:

Test output to stdout
Test output to cout

// Attempt #2
// Replace the cout buffer through cout's rdbuf
// Call sync_with_stdio and synchronize stdout/cout
// (hopefully making the stringbuf the common buffer)
// function and try.

#include <stdio.h>
#include <iostream>
#include <string>
#include <sstream>

int main ()
{
  std::stringstream ss;

  std::streambuf* coutbuf = std::cout.rdbuf ();

  std::streambuf* stringbuf = ss.rdbuf ();

  std::cout.rdbuf (stringbuf);

  std::ios::sync_with_stdio ();

  std::cout << "Test output to cout\n";

  printf ("Test output to stdout\n");

  std::cout.rdbuf (coutbuf);

  std::cout << ss.str ();
}

With Borland 5.5 I get:

Test output to stdout
Test output to cout

What does the standard have to say on this? And how can i achieve
what i want?

Thanks in advance,
Sai Shankar

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]