473,698 Members | 2,554 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to capture stdout temporarily?

Hi,

Let say that I have a function like
void writeHello() {
printf("Hello") ;
}

Now I need to make an automated test fot that function.
The test function returns 1 for successful and 0 for unsuccessful test.
int Test_writeHello () {
...
writeHello();
...
}

Test funtion is called from test driver that reports to stdout how it went.
E.g. "Test case 10/20 OK Test_writeHello ()"
I can redirect stdout to a file (freopen("file" ,"w",stdout) ) and then check
if the function works but how can I restore stdout back to the screen?

- Santtu
May 4 '06 #1
9 18284
"Santtu Nyrhinen" <sa************ *@nokia.com> wrote:
# Hi,
#
# Let say that I have a function like
# void writeHello() {
# printf("Hello") ;
# }
#
# Now I need to make an automated test fot that function.
# The test function returns 1 for successful and 0 for unsuccessful test.
# int Test_writeHello () {
# ...
# writeHello();
# ...
# }
#
# Test funtion is called from test driver that reports to stdout how it went.
# E.g. "Test case 10/20 OK Test_writeHello ()"
# I can redirect stdout to a file (freopen("file" ,"w",stdout) ) and then check
# if the function works but how can I restore stdout back to the screen?

If you're using Unix, it might be easier to fork and redirect output
in the child process only.
--
SM Ryan http://www.rawbw.com/~wyrmwif/
Don't say anything. Especially you.
May 4 '06 #2

Santtu Nyrhinen wrote:
Hi,

Let say that I have a function like
void writeHello() {
printf("Hello") ;
}

Now I need to make an automated test fot that function.
The test function returns 1 for successful and 0 for unsuccessful test.
int Test_writeHello () {
...
writeHello();
...
}

Test funtion is called from test driver that reports to stdout how it went.
E.g. "Test case 10/20 OK Test_writeHello ()"
I can redirect stdout to a file (freopen("file" ,"w",stdout) ) and then check
if the function works but how can I restore stdout back to the screen?


Have you considered using `stderr` for the test function. Most OSes can
redirect `stdout` and `stderr` to separate files/screens.

May 4 '06 #3
Santtu Nyrhinen wrote:
Hi,

Let say that I have a function like
void writeHello() {
printf("Hello") ;
}

Now I need to make an automated test fot that function.
The test function returns 1 for successful and 0 for unsuccessful
test. int Test_writeHello () {
...
writeHello();
...
}

Test funtion is called from test driver that reports to stdout how it
went. E.g. "Test case 10/20 OK Test_writeHello ()"
I can redirect stdout to a file (freopen("file" ,"w",stdout) ) and then
check if the function works but how can I restore stdout back to the
screen?


On XP and using gcc, the following works

freopen("CON:", "w",stdout) ;

If you're on Linux et al, maybe you or someone else could try the following?

freopen("/dev/tty", "w",stdout) ;
--
==============
Not a pedant
==============
May 4 '06 #4
pemo wrote:
Santtu Nyrhinen wrote:
Hi,

Let say that I have a function like
void writeHello() {
printf("Hello") ;
}

Now I need to make an automated test fot that function.
The test function returns 1 for successful and 0 for unsuccessful
test. int Test_writeHello () {
...
writeHello();
...
}

Test funtion is called from test driver that reports to stdout how it
went. E.g. "Test case 10/20 OK Test_writeHello ()"
I can redirect stdout to a file (freopen("file" ,"w",stdout) ) and then
check if the function works but how can I restore stdout back to the
screen?


On XP and using gcc, the following works

freopen("CON:", "w",stdout) ;

If you're on Linux et al, maybe you or someone else could try the following?

freopen("/dev/tty", "w",stdout) ;
--
==============
Not a pedant
==============


This works for me (your results may vary):
#include <stdio.h>

int main (int argc, char* argv[])
{
freopen ("test.txt", "w", stdout);
printf ("hello file!\n");
freopen ("/dev/tty", "w", stdout);
printf ("hello world!\n");
return 0;
}

May 4 '06 #5
In article <11************ *********@u72g2 000cwu.googlegr oups.com>,
ra*********@gma il.com <ra*********@gm ail.com> wrote:
This works for me (your results may vary):
#include <stdio.h>

int main (int argc, char* argv[])
{
freopen ("test.txt", "w", stdout);
printf ("hello file!\n");
freopen ("/dev/tty", "w", stdout);
printf ("hello world!\n");
return 0;
}


This does not work for me. Here is what happened when I ran it:

% ./rageratwork > output
hello world!
% cat test.txt
hello file!
% cat output
%

The "captured" file ("test.txt") worked OK but the "output" file
was empty, and the output that *should* have been there appeared
on my virtual console.

(There *is* a way to make it "work" on a Unix-like system, but this
is off-topic here. Hint: ask about dup2() in comp.unix.progr ammer.
Arguably the best fix is to rewrite the code in question to take
a "FILE *" parameter, rather than assuming all its output is to go
to stdout. This can be done in completely portable C code, instead
of using system-specific hacks that have to be rewritten every time
you move to another system.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
May 4 '06 #6
Chris Torek wrote:
In article <11************ *********@u72g2 000cwu.googlegr oups.com>,
ra*********@gma il.com <ra*********@gm ail.com> wrote:
This works for me (your results may vary):
#include <stdio.h>

int main (int argc, char* argv[])
{
freopen ("test.txt", "w", stdout);
printf ("hello file!\n");
freopen ("/dev/tty", "w", stdout);
printf ("hello world!\n");
return 0;
}


This does not work for me. Here is what happened when I ran it:

% ./rageratwork > output
hello world!
% cat test.txt
hello file!
% cat output
%

The "captured" file ("test.txt") worked OK but the "output" file
was empty, and the output that *should* have been there appeared
on my virtual console.

(There *is* a way to make it "work" on a Unix-like system, but this
is off-topic here. Hint: ask about dup2() in comp.unix.progr ammer.
Arguably the best fix is to rewrite the code in question to take
a "FILE *" parameter, rather than assuming all its output is to go
to stdout. This can be done in completely portable C code, instead
of using system-specific hacks that have to be rewritten every time
you move to another system.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.


I'd have to say the stdout you were redirecting to the output file was
closed at the first freopen hence, no ouput. If you put a printf before
the first freopen obviously you will see it in the redirected output
file.

Try this once:
#include <stdio.h>

int main (int argc, char* argv[])
{
FILE* f;

printf ("hello stdout!\n"); // stdout

f = fopen("/dev/stdout", "a");
fprintf (f, "hello /dev/stdout!\n"); // also stdout

freopen ("test.txt", "w", stdout);
printf ("hello file!\n"); // to the file

fprintf (f, "hello /dev/stdout!\n"); // still goes to the
redirected file

freopen ("/dev/tty", "w", stdout);
printf ("hello world!\n"); // back to the terminal

fprintf (f, "hello /dev/stdout! %d\n", fileno(f)); // still
goes to the redirected file
fclose (f);
return 0;
}

[rager]$ ./test > output
hello world!
[rager]$ cat test.txt
hello file!
[rager]$ cat output
hello stdout!
hello /dev/stdout!
hello /dev/stdout!
hello /dev/stdout! 3

Of course I completely agree that your portable C is the best solution
and the way I've always aproached this type of situation. Just thought
it was an interresting exercise...

Dave.

May 4 '06 #7
ra*********@gma il.com wrote:
Chris Torek wrote:
In article <11************ *********@u72g2 000cwu.googlegr oups.com>,
ra*********@gma il.com <ra*********@gm ail.com> wrote:
This works for me (your results may vary):
#include <stdio.h>

int main (int argc, char* argv[])
{
freopen ("test.txt", "w", stdout);
printf ("hello file!\n");
freopen ("/dev/tty", "w", stdout);
printf ("hello world!\n");
return 0;
}
This does not work for me. Here is what happened when I ran it:

% ./rageratwork > output
hello world!
% cat test.txt
hello file!
% cat output
%

The "captured" file ("test.txt") worked OK but the "output" file
was empty, and the output that *should* have been there appeared
on my virtual console.

(There *is* a way to make it "work" on a Unix-like system, but this
is off-topic here. Hint: ask about dup2() in comp.unix.progr ammer.
Arguably the best fix is to rewrite the code in question to take
a "FILE *" parameter, rather than assuming all its output is to go
to stdout. This can be done in completely portable C code, instead
of using system-specific hacks that have to be rewritten every time
you move to another system.)
--


<snip>
Of course I completely agree that your portable C is the best solution
and the way I've always aproached this type of situation. Just thought
it was an interresting exercise...


Portable - what, where - in the **ux world only if you mean dup2 I believe?
--
==============
Not a pedant
==============
May 5 '06 #8
pemo wrote:
Chris Torek wrote:
[snip]
Arguably the best fix is to rewrite the code in question to take
a "FILE *" parameter, rather than assuming all its output is to go
to stdout. This can be done in completely portable C code, instead
of using system-specific hacks that have to be rewritten every time
you move to another system.)
[snip] ra*********@gma il.com <ra*********@gm ail.com> wrote:
Of course I completely agree that your portable C is the best solution
and the way I've always aproached this type of situation. Just thought
it was an interresting exercise...


Portable - what, where - in the **ux world only if you mean dup2 I believe?


I believe Chris's solution is quite portable and has nothing to do with
duping any file descriptors. Pass in stdout or any other open file as a
parameter depending where you want the output to go.

Dave.

May 5 '06 #9
ra*********@gma il.com wrote:
pemo wrote:
Chris Torek wrote:
[snip] Arguably the best fix is to rewrite the code in question to take
a "FILE *" parameter, rather than assuming all its output is to go
to stdout. This can be done in completely portable C code, instead
of using system-specific hacks that have to be rewritten every time
you move to another system.)
[snip] ra*********@gma il.com <ra*********@gm ail.com> wrote: Of course I completely agree that your portable C is the best
solution and the way I've always aproached this type of situation.
Just thought it was an interresting exercise...


Portable - what, where - in the **ux world only if you mean dup2 I
believe?


I believe Chris's solution is quite portable and has nothing to do
with duping any file descriptors. Pass in stdout or any other open
file as a parameter depending where you want the output to go.


True - sorry, too quick off the mark!
--
==============
*Not a pedant*
==============
May 5 '06 #10

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

Similar topics

3
5507
by: Alfred von Campe | last post by:
I would think this is simple to do, but I just can't figure it out this morning. Assume I have a function in Perl which writes some output to STDOUT. I want to call this function from a Perl script and capture its output in a variable. If this was an executable or even another Perl script, I could use the open() function. But since this is a Perl function, I can't use open(). Or can I? There's got be be a way, but my brain is just...
3
3044
by: Dave | last post by:
Hi, I have a control on my vb app form that dont cath a mouse event`s how can i catch a mouse event on that control and pass it to a function in my main form??? In VB-6 i used the setcapture api function! Thanks
4
3254
by: AxOn | last post by:
Hi! Has anybody encountered console exe's that are impossible to get stdOut from? Does anybody know if there are different(other then string text) ways that console exe files send out their outputs??
2
2306
by: ChaosKCW | last post by:
Hi Has anyone caputerd the output from the std ftp lib? It seems a bit annoying that everything is printed to stdout. It means incorporating this into any real program is a problem. It would have been much better if they used the std logging module and hooked up a console logger for the feault ftp application. Alas it isnt that way. Capturing stdout like this??? :
0
1044
by: jfigueiras | last post by:
Hi all! I have a problem with the module subprocess! The problem is that the external software that I am running under subprocess.Popen opens stdin, stdout, stderr, and other file descriptors to write and read in the hard drive. I know how to capture stdin, stdout and stderr, what I cannot do is to capture the other file-descriptors. Is there any way to wrap those non-standard file descriptors and make them write and read from a...
2
2847
by: Gerard Flanagan | last post by:
Hello, I have a third party shell script which updates multiple environment values, and I want to investigate (and ultimately capture to python) the environment state after the script has run. But running the script as a child process only sets values for that process, which are lost after execution. So I thought I could simply tack on an 'env' command line to the script input lines as shown below. However, using subprocess.Popen gives...
2
3093
by: jdbartlett | last post by:
I'm trying to capture output from a command line utility (XMLSec), but only get an empty result. If I call the script from the command line, I see XMLSec's output, but I can't seem to capture it! My PHP installation is working correctly and captures other command line output just fine, XMLSec is the only exception I've found. I've also tried a couple of other systems to confirm this behavior. Capture methods I've tried include:
5
8975
by: Joakim Hove | last post by:
Hello, I have written a program in C; this programs uses an external proprietary library. When calling a certain function in the external library, the particular function writes a message to stdout. I am not particularly interested in this message, and would like to silence it - however I do not know how to do it. (I stdout and stderr my self, so just redirecting into oblivion is not an option). An excerpt of the code looks like this:
13
1960
by: Jim Langston | last post by:
I had asked this in comp.lang.c++ with out any answers that would actually work, so I'm hoping someone here may know a way. I am calling C library functions that want to output to stdout. I need to capture this to memory for use internally inside the program without modifying the library calls that actually do the output. This is for the GraphViz libraries and it is the output of dot that I'm actually trying to capture. I have...
0
8685
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
8612
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,...
0
9171
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8905
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
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2342
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.