473,416 Members | 1,725 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,416 software developers and data experts.

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 18250
"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*********************@u72g2000cwu.googlegroups. com>,
ra*********@gmail.com <ra*********@gmail.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.programmer.
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*********************@u72g2000cwu.googlegroups. com>,
ra*********@gmail.com <ra*********@gmail.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.programmer.
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*********@gmail.com wrote:
Chris Torek wrote:
In article <11*********************@u72g2000cwu.googlegroups. com>,
ra*********@gmail.com <ra*********@gmail.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.programmer.
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*********@gmail.com <ra*********@gmail.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*********@gmail.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*********@gmail.com <ra*********@gmail.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
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...
3
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...
4
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...
2
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...
0
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...
2
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....
2
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!...
5
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...
13
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.