close(1) 1 to state stdout

     Example 1: Reassign a file descriptor.

     The following example closes the file descriptor  associated
     with  standard  output  for  the current process, re-assigns
     standard output to a new file  descriptor,  and  closes  the
     original  file  descriptor to clean up. This example assumes
     that the file descriptor 0,  which  is  the  descriptor  for
     standard input, is not closed.

     #include <unistd.h>
     ...
     int pfd;
     ...
     close(1);
     dup(pfd);
     close(pfd);
     ...

     Incidentally, this is exactly what could be achieved using:

     dup2(pfd, 1);
     close(pfd);

posted on 2012-02-29 10:38  Richard.FreeBSD  阅读(216)  评论(0编辑  收藏  举报

导航