472,984 Members | 1,957 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,984 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 5675
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.