469,575 Members | 1,695 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,575 developers. It's quick & easy.

Suppressing stdout output

Let's say I want to call a function but suppress what it writes to
stdout. One way that works on my machine is this:

#include <stdio.h>
int main(void) {
freopen("/dev/null", "w", stdout);
puts("Hello");
return 0;
}

but how do I restore the original stdout in a portable way? And is
there a portable way to avoid using /dev/null? I could write to a
temporary file, but that uselessly fills up the hard drive.

Thanks for your thoughts.
-Peter
Nov 14 '05 #1
3 5615
Peter Ammon wrote:
Let's say I want to call a function but suppress what it writes to
stdout. One way that works on my machine is this:

#include <stdio.h>
int main(void) {
freopen("/dev/null", "w", stdout);
puts("Hello");
return 0;
}

but how do I restore the original stdout in a portable way? And is
there a portable way to avoid using /dev/null? I could write to a
temporary file, but that uselessly fills up the hard drive.


This is Question 12.34 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

.... and you're not going to like the answer.

--
Er*********@sun.com

Nov 14 '05 #2
Eric Sosman wrote:
Peter Ammon wrote:
Let's say I want to call a function but suppress what it writes to
stdout. One way that works on my machine is this:

#include <stdio.h>
int main(void) {
freopen("/dev/null", "w", stdout);
puts("Hello");
return 0;
}

but how do I restore the original stdout in a portable way? And is
there a portable way to avoid using /dev/null? I could write to a
temporary file, but that uselessly fills up the hard drive.

This is Question 12.34 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

... and you're not going to like the answer.


Oops, I thought I had pretty much memorized what was in the FAQ. Thanks.
Nov 14 '05 #3
In <ca**********@news.apple.com> Peter Ammon <pe*********@rocketmail.com> writes:
Let's say I want to call a function but suppress what it writes to
stdout. One way that works on my machine is this:

#include <stdio.h>
int main(void) {
freopen("/dev/null", "w", stdout);
puts("Hello");
return 0;
}

but how do I restore the original stdout in a portable way?
You cannot.
And is there a portable way to avoid using /dev/null?
Nope.
I could write to a
temporary file, but that uselessly fills up the hard drive.


The idea is to use the 'f' flavour of the output functions (e.g. fprintf
and fputs) and to manipulate the FILE pointer variable they use.

#define NULLDEVICE "/dev/null"

FILE *nulldev = fopen(NULLDEVICE, "w");
FILE *output;

Do all your "suppressable" output via the "output" FILE pointer and
set it to either nulldev or stdout, according to your needs. Most
platforms support the concept of null device, but, of course, you have
to define the NULLDEVICE macro as appropriate.

If you want to avoid this portability issue, you can set "output" to
either stdout or NULL. In this case, however, each output call must
be prefixed by "if (output != NULL)". This can be trivially handled with
wrapper functions (or even macros, for non-variadic functions):

#define FPUTS(string, stream) (stream != NULL ? fputs(string, stream) : 0)

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Phil Powell | last post: by
4 posts views Thread by Wes S. | last post: by
9 posts views Thread by Santtu Nyrhinen | last post: by
20 posts views Thread by David Mathog | last post: by
7 posts views Thread by MisterPete | last post: by
1 post views Thread by =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post: by
5 posts views Thread by amit.uttam | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.