472,786 Members | 1,317 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,786 software developers and data experts.

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 6580
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Phil Powell | last post by:
How do I suppress the lines PHP normally delivers to stdout (your browser) if I am doing command-line PHP? e.g. stuff.php: <? echo 'Hello World'; ?> Calling it from a Red Hat 7.3...
4
by: Wes S. | last post by:
How can I redirect and tag sys.stdout values. I tried creating a class module that saved the old stdout and replaced it, but I can't seem to figure out how to do such. Here is what I got... ...
1
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
4
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
9
by: Santtu Nyrhinen | last post by:
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...
20
by: David Mathog | last post by:
A program of mine writes to a tape unit. Output can be either through stdout or through a file opened with fopen(). When all the data is transferred to tape the program needs to close the output...
7
by: MisterPete | last post by:
How can I inherit from file but stil create an instance that writes to stdout? ----------- I'm writing a file-like object that has verbosity options (among some other things). I know I could just...
1
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, I have a C# application in which I start another process which produces output to stdout and stderr. In fact, that process is the uSoft VS2005 C/C++ compiler itself! I would like to...
5
by: amit.uttam | last post by:
Hey everyone, I've recently jumped big time into python and I'm working on a software program for testing automation. I had a question about proper logging of output. What I would like is: 1....
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 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: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
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
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.