re open stdout

re open stdout

Post by Herve Couppe de Lahongrais - CD » Sat, 07 Jun 1997 04:00:00

> Hi,

>   I am attempting to temporarily close stdout, do some processing (that
> has results sent to stdout, and I don't want users to see) and then do
> more processing that has results that I want the user to see ...

>   I have tried working with dup2, fdopen, freopen, but I haven't got the
> right combination.  Do I need to use fcntl or ioctl ?

> Thanks,
> Ken Sullivan

With such functions like dup2, fcntl, ioctl, I'm supposing you are on a
Unix system, so I followup this post to comp.unix.programmer.
Here is a sample program, I hope it helps you :

#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>

int     main(int argc, char *argv[])

        int     stdout_fileno;
        int     null_fileno;

        printf("Hello ");

        ** dup & close STDOUT_FILENO
        ** Don't fclose stdout because it could not be reopened
        ** Flushing stdout is necessary because it is line-buffered by
        ** default. If this was not done, the above two strings would be
        ** because the string "world" is not terminated by a new-line
        if ((stdout_fileno = dup(STDOUT_FILENO)) < 0) {

        ** Redirect STDOUT_FILENO to /dev/null to make printf happy
        if ((null_fileno = open("/dev/null", O_WRONLY)) < 0) {
        if (dup2(null_fileno, STDOUT_FILENO) < 0) {

        printf("Goobye cruel world");

        ** restore STDOUT_FILENO & close useless stdout_fileno
        ** Flush stdout to clear internal buffer before restoring
        if (dup2(stdout_fileno, STDOUT_FILENO) < 0) {

        printf("\nHello, I'm working again\n");


This program prints :
Hello world
Hello, I'm working again

Herve Couppe de Lahongrais (SEU)       | Eurocontrol Experimental Centre


1. Q: How to re-open stdout after closing it

I need to re-direct stdout temporarily and then restore it to
its original behavior.  The re-directing part is easy with
close(STDOUT_FILENO), dup(fd), and close(fd).  I cannot, however,
figure out how to re-open stdout so that output goes where it did
before I did the close(STDOUT_FILENO).

If you could help and it wouldn't be much trouble, would you
please email me with a solution?  I'll be glad to summarize to
the net.

Thanks for reading this,

Mark Heroux

"Steel can be any shape you want if you are skilled enough and
any shape but the one you want if you are not."  Robert Pirsig

2. Problems with Solaris

3. redirecting STDOUT of a bg process to STDOUT of a new xterm


5. Writing ksh stdout to both file and stdout?

6. Memory

7. file descriptor Q, redirecting stderr and stdout to stdout and file.

8. Burning cd from liunix distribution from FTP

9. OT Open source = Open mind?

10. What percentage of open-source programmers are paid for working on open-source projects?

11. OpenWin Filemgr: 'cannot open file: too many files open' ?

12. Open Desktop VS Open Enterprise

13. Announce: Motif release to Open Source Community leads to Open Motif Everywhere on PPC