Redirected cout: How to Tell?

G

George B

When I redirect standard output, I would like to duplicate a few lines of
this output to the console. If I add duplicates of the desired source
statements, changing cout to cerr, it works fine, but I end up with
duplicated lines when the program is executed without redirection. Is there
a way to test cout to tell whether its output is being redirected?

Like,
cout << "This information is very important" << endl;
if (cout_is_redirected) {
cerr << "This information is very important" << endl;
}

Thanks,

George
 
M

Mike Wahler

George B said:
When I redirect standard output, I would like to duplicate a few lines of
this output to the console. If I add duplicates of the desired source
statements, changing cout to cerr, it works fine, but I end up with
duplicated lines when the program is executed without redirection. Is there
a way to test cout to tell whether its output is being redirected?

Like,
cout << "This information is very important" << endl;
if (cout_is_redirected)

Set this condition ('cout_is_redirected') to 'true'
when you do the redirection.

{
cerr << "This information is very important" << endl;
}

-Mike
 
T

Thomas Matthews

George said:
When I redirect standard output, I would like to duplicate a few lines of
this output to the console. If I add duplicates of the desired source
statements, changing cout to cerr, it works fine, but I end up with
duplicated lines when the program is executed without redirection. Is there
a way to test cout to tell whether its output is being redirected?

Like,
cout << "This information is very important" << endl;
if (cout_is_redirected) {
cerr << "This information is very important" << endl;
}

Thanks,

George

My understanding is that there is no method or functionality
to determine if "cout" has been redirected. Some platforms
allow for it to be redirected, others don't.

Many programs will have a "default" output stream. The program
would assign this to point to cout. It could be changed to
point to another output stream, such as a file. This would be
how a conforming program could redirect output. Also, one
may be able to tell if the output has been redirected by
comparing the value of the pointer.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
I

Ivan Vecerina

George B said:
When I redirect standard output, I would like to duplicate a few lines of
this output to the console. If I add duplicates of the desired source
statements, changing cout to cerr, it works fine, but I end up with
duplicated lines when the program is executed without redirection. Is
there
a way to test cout to tell whether its output is being redirected?

Like,
cout << "This information is very important" << endl;
if (cout_is_redirected) {
cerr << "This information is very important" << endl;
}

When you redirect cout, could you as well redirect cerr to a "tee" streambuf
that will automatically send output to both the console an your file?

Dietmar Kühl used to provide a tee-streambuf, but the link to the
archive that once contained it seems to be broken (see bottom of
page http://www.informatik.uni-konstanz.de/~kuehl/iostream/).


hth,
Ivan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,269
Latest member
vinaykumar_nevatia23

Latest Threads

Top