473,324 Members | 2,456 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,324 software developers and data experts.

Redirecting all stdout commands

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 technically create an executable that calls the function and then
using a command prompt call the exexutable using the > output.txt
redirector but I would prefer to do everything within the application
itself. Is there a way for force an output to screen to a file or
string without having access to the cout call itself? ( would be easy
to do if I could simply do outfile << myText but the output is coming
from a function in a DLL for example).

example:

OutputMethod() // Inaccessible code that can not be modified
{
std::cout << "This is some text" << std::endl;
}

int main() // My Code
{
...
Redirect stdout to a text file
...
OutputMethod(); // Call the method and have it output to a file
instead of the screen
...
Verify the output file to see it's contents
...
}

Thanks,
Adam

May 4 '06 #1
8 5733
Morpheus wrote:
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 technically create an executable that calls the function and
then using a command prompt call the exexutable using the > output.txt
redirector but I would prefer to do everything within the application
itself. Is there a way for force an output to screen to a file or
string without having access to the cout call itself? ( would be easy
to do if I could simply do outfile << myText but the output is coming
from a function in a DLL for example).

example:

OutputMethod() // Inaccessible code that can not be modified
{
std::cout << "This is some text" << std::endl;
}

int main() // My Code
{
...
Redirect stdout to a text file
...
OutputMethod(); // Call the method and have it output to a file
instead of the screen
...
Verify the output file to see it's contents
...
}


Isn't there something about this in the FAQ? If not, google for
"redirecting standard output". It's been done countless times.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 4 '06 #2
I am sure it has been done, in fact I think I remember doing this back
some 10 years ago in C but redirecting to a printer. Have you actually
tried that search in Google? All you get are references to the >
operator in a command line environment. Unless you misunderstood the
question...

I will keep looking thought the FAQ unless someone else has it fresh in
their minds and can help.

May 4 '06 #3
Morpheus wrote:
I will keep looking thought the FAQ unless someone else has it fresh in
their minds and can help.


You can replace cout's streambuf with an own one that writes the data where
you want it. If you don't want to limit it to cout, but also C stdout,
there is no standard C+++ way. On a POSIX system, you might want to have a
look at the dup2() function.
May 4 '06 #4
Thanks Rolf, the dup2 method seems to work perfectly for the cout
calls. If I want to do the same for C's printf, is there no way to
accomplish this?

The code: (albeit not pretty - found on the web )
-----------------------------------------
int old;
FILE *DataFile;

old = _dup( 1 ); /* "old" now refers to "stdout" */
DataFile = fopen( "C:\\data.txt", "w" );
/* stdout now refers to file "data" */
if( -1 == _dup2( _fileno( DataFile ), 1 ) )
{
perror( "Can't _dup2 stdout" );
exit( 1 );
}
std::cout << "Test";

/* Flush stdout stream buffer so it goes to correct file */
fflush( stdout );
fclose( DataFile );
_dup2( old, 1 );
-----------------------------------------

When doing this the cout is redirected to the text file I specified.
My only problem now is the older code. The classes under test all
derive from a base class that defines a method called dump() which
dumps thier contents to the screen. The older classes implementing
this method are using printf while the newer ones are using cout.
Knowing which does what I can tweak the tests to use the right redirect
for each class. Only problem is I have not been successful doing this
with a printf. Any suggestions?

May 5 '06 #5

"Morpheus" <ad***********@gmail.com> wrote in message news:11********************@g10g2000cwb.googlegrou ps.com...
I am sure it has been done, in fact I think I remember doing this back
some 10 years ago in C but redirecting to a printer. Have you actually
tried that search in Google? All you get are references to the >
operator in a command line environment. Unless you misunderstood the
question...

I will keep looking thought the FAQ unless someone else has it fresh in
their minds and can help.


Look at

Redirecting cout/cerr <--> file : streambuf-method
http://groups.google.com/group/alt.s...4204134e9956a0

Redirecting cin <--> file
http://groups.google.com/group/alt.s...1d1032e2915ffd

Redirecting stdout <--> file
http://groups.google.com/group/alt.s...fdaef5e8fd79b5

Redirecting stdin <--> file
http://groups.google.com/group/alt.s...a6267142360edb
Also:
http://alexvn.freeservers.com/s1/download.html
* Redirecting cout/cerr <--> file
* Redirecting cin <--> file
* Redirecting stdout <--> file
* Redirecting stdin <--> file
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

May 5 '06 #6
Shoot... I got really exited when I saw this but then noticed it was
Unix specific. I am running a Win32 system and the same calls do not
seem to work. Should I report this on a Win32 specific site?

May 5 '06 #7
Morpheus wrote:
Shoot... I got really exited when I saw this but then noticed it was
Unix specific. I am running a Win32 system and the same calls do not
seem to work. Should I report this on a Win32 specific site?


Look up freopen()
May 5 '06 #8
Morpheus wrote:
Shoot... I got really exited when I saw this but then noticed it was
Unix specific. I am running a Win32 system and the same calls do not
seem to work. Should I report this on a Win32 specific site?


Yes, it is UNIX-specific.
You have three options:
1) Find (write) similar _platform-independent_ program;
2) Use Cygwin on Win32 (I compiled my UNIX-specific program with Cygwin
GNU g++ on Win32);
3) Ask your question on microsoft.* newsgroups.

Regards,

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

May 7 '06 #9

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

Similar topics

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...
4
by: Alexander Stippler | last post by:
Hi, The short story: I have to redirect stdout (not cout!!) to an ostream. How can I manage this? The long story: I use the NAGC numerics library with C++ and want to use its output routines...
0
by: Elad | last post by:
Hello All, I am trying to capture some printf's from a C function called by python. <Working under winxp> I have tried to following: STDOUT = 1 # stdout fd (re, we) = os.pipe() #...
1
by: pp4175 | last post by:
Hello Everyone, I am currently working on writing a GUI wrapper for a Menu driven DOS Program. What I was looking on doing is redirecting stdout/stdin to a stream and read/write similar to a...
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).
4
by: ElderGeek | last post by:
Hi, all. I am coding in C on Linux, and using an external library provided by others. This library generates a batch of error/warning messages to stdout, which I do not want to see. Before...
0
by: Tom Gaudasinski | last post by:
Greetings, I'm trying to redirect python's stdout to another location. The reason for this is that I'm embedding python in an application. Now, originally my code was developed for Linux and that...
0
by: kreismaler | last post by:
I have some problems to understand the difference of using the STDOUT and using "anonymous pipes" as shown below: using System; using System.Diagnostics; using System.IO; namespace...
1
by: n00b | last post by:
greetings, i would like to redirect stdout/err to a mysql table and would like a) some peer review and b) suggestions for hardening the approach for a general purpose class. thank you very much....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.