472,356 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Redirected cout: How to Tell?

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
Jul 22 '05 #1
3 2108

"George B" <gh*******@hotmail.com> wrote in message
news:1102124813.DqCKgGe0dbvkO1bGScIksg@teranews...
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
Jul 22 '05 #2
George B wrote:
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

Jul 22 '05 #3
"George B" <gh*******@hotmail.com> wrote in message
news:1102124813.DqCKgGe0dbvkO1bGScIksg@teranews...
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 Khl 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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 22 '05 #4

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

Similar topics

8
by: Philip Lawatsch | last post by:
Hi, I'd like to get rid of all the cout stuff in my code. Following situation: Way over 2000 files, search and replace in all of them is not an option. All of the files include a basic...
6
by: Omid | last post by:
Hi. I have problems when I try to redirect everything that is sent to cout to a file. I have one piece of code that works and one that does not work. The only difference is which headers I use....
1
by: iceColdFire | last post by:
HI, I have an MFC application...I have used Memory Leak functions, as CMemoryState a,b; Now, a.DumpStatistics() send all its output to the console.... I need to get this output into a...
3
by: Vikrant | last post by:
Friends, Due an application (old) install program problem, Under AIX I could only create DB2 instance, I could also catalog it. Its complex application and I am not expert in creating full...
9
by: GL | last post by:
I am running DB2 8.1.1 on AIX 5.1 Having a problem with a redirected restore. Once into the restore continue phase, I immediately get the following SQL2059W A device full warning was...
8
by: Julian | last post by:
hi, i have the following piece of code in a program compiled using both VC8 and VC6. it basically just writes out two arrays to cout. the VC6 program takes about 4 seconds to go through that bit of...
4
by: Opa | last post by:
Hi, I would like to know if there is an event that will tell me when a page is being redirected and also the url of where the page is being redirected. Is there an event that is fired which will...
8
by: Prateek | last post by:
Hey... Can anybody tell me why "<<" operator is used behind cout? Like: cout<<"Welcome"; My one friend told me that it "shifts the bits"...But it's not very clear to me...Can anybody tell?
3
by: worlman385 | last post by:
why when I use cout, I must put - using std::cout; #include <iostream> using std::cout; if i look at iostream, it seem cout is defined in iostream - __PURE_APPDOMAIN_GLOBAL extern _CRTDATA2...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...

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.