473,387 Members | 1,624 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,387 software developers and data experts.

Redirecting printed messages to a file

Here is what I use to redirect printed messages to a file:

std::ofstream file("logfile");
std::cout.rdbuf(file.rdbuf());

std::cout << "this message goes to a file" << std::endl;

Will the next message I print go to a file as well?

std::printf("does this message go to a file?");

How do I set things up so that both std::cout and std::printf redirect their
output to the same file? Thanks.
Sep 16 '05 #1
2 11043
Jason Heyes wrote:
Here is what I use to redirect printed messages to a file:

std::ofstream file("logfile");
std::cout.rdbuf(file.rdbuf());

std::cout << "this message goes to a file" << std::endl;
Do it this way:

file << "this message goes to a file\n";

Will the next message I print go to a file as well?

std::printf("does this message go to a file?");
Nope.

fprintf(handle, "does this message go to a file? Yes.");

How do I set things up so that both std::cout and std::printf redirect their
output to the same file? Thanks.


If you want to write to a file, write to a file. If you want to write to
the console, write to cout or stdout. They write to the console, and
it's up to the user to redirect them.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Sep 16 '05 #2
Pete Becker wrote:
Jason Heyes wrote:
Here is what I use to redirect printed messages to a file:

std::ofstream file("logfile");
std::cout.rdbuf(file.rdbuf());

std::cout << "this message goes to a file" << std::endl;

Do it this way:

file << "this message goes to a file\n";

Will the next message I print go to a file as well?

std::printf("does this message go to a file?");

Nope.

fprintf(handle, "does this message go to a file? Yes.");

How do I set things up so that both std::cout and std::printf redirect
their output to the same file? Thanks.

If you want to write to a file, write to a file. If you want to write to
the console, write to cout or stdout. They write to the console, and
it's up to the user to redirect them.


I agree with him, but if you really want to redirect the standard
streams, you can use std::freopen, e.g.

#include <cstdio>
#include <iostream>

std::freopen("stdout.txt", "w", stdout);
std::freopen("stderr.txt", "w", stderr);

std::cout << "this will be written to stdout.txt\n";
std::printf("so will this...\n");

std::cerr << "this will be written to stderr.txt\n";
std::fprintf(stderr, "so will this...\n");

I'd stay away from this unless you REALLY want ALL stdout and stderr
output redirected to a file. Much better to use ofstream with a log
file. The original streams get closed when you use freopen.

--John Ratliff
Sep 16 '05 #3

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

Similar topics

2
by: Birch | last post by:
I have a python script that uses the print function throughout, and as well uses calls to os.system() to spawn DOS commandline executables. My issue is that I redirect all of the output from this...
3
by: David Douard | last post by:
Hi everybody, let me explain by problem: I am working on an application which consists in a C++ dll (numeric computations) and a Python IHM (Python/Tk), which must run under Linux and win32. My...
3
by: Laszlo Zsolt Nagy | last post by:
Hello, I have this code: s = smtplib.SMTP() s.set_debuglevel(1) s.connect(host=smtp_host) s.set_debuglevel(0) log("Connected, sending e-mail") sys.stdout.flush()
8
by: Morpheus | last post by:
I am trying to test a function that outputs text to the standard output, presumably using a cout call. I do not have access to the source, but need to test the output. Is this possible? I can...
18
by: praetor.michael | last post by:
I have a DLL written in C that writes to stderr. I have a win32 console application that makes calls to the DLL. In the console app I redirect stderr to a file using freopen. The problem I'm...
10
by: SamG | last post by:
How could i make, from inside the program, to have the stdout and stderr to be printed both to a file as well the terminal(as usual).
116
by: dmoran21 | last post by:
Hi All, I am working on a program to take input from a txt file, do some calculations, and then output the results to another txt file. The program that I've written compiles fine for me, however,...
10
by: Guillaume Dargaud | last post by:
Hello all, I have some error checking using the function 'perror', which writes messages on stderr. I'd like to send all error messages to a file instead. Is there some way to do this, short of...
6
by: drhilbert | last post by:
Dear all, in these days, we have found a problem we can't solve even after long long googling, so we are here asking your precious help. In our program instead of using cout to print messages...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.