473,720 Members | 2,814 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5782
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
"redirectin g 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.t xt", "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******** ************@g1 0g2000cwb.googl egroups.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
3757
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 problem is the C++ lib does write stuffs on its stdout, and I would like to print those messages in a Tk frame. When I run the computation, it has it's own thread. So my question is : how van I redirect the dll's stdout to something I can
4
6304
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 for e.g. complex matrices. The problem: You can either print to a file (given by filename) or to stdout. To call the NAGC-method within an operator<< thus I have to redirect stdout to the given
0
1573
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() # Create re / write handlers
1
2594
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 file. My issue is that I try to read from the stdout stream and never get anything only if the program has exited. Is there a way of reading the redirected stdout from a DOS program in say real time? I have tried the async (BeginOutputReadLine) and...
10
7629
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
9713
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 each function call to the external library, I can insert the following: freopen("/dev/null", "w", stdout); However, after the function call I cannot figure out how to reroute stdout to the console. I tried early in the program defining: FILE...
0
1829
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 did not require redirection due to the fact that every X11 application can have an STDOUT associated with it. I then proceeded to take interest in making my code portable and looked at compiling it on windows, however Win32 GUI applications are...
0
2603
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 ProcessTest { class Program
1
3321
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. import sys import MySQLdb class DBLogger(object): def __init__(self):
0
8833
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8727
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9126
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9055
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6661
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Duprť who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4482
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4748
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3186
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2567
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.