dup and dup2
时间:2006-03-18 来源:deb
dup, dup2 - duplicate a file descriptor
#include <unistd.h>
int dup(int oldfd);
int dup2(int oldfd, int newfd);
dup and dup2 create a copy of the file descriptor oldfd.
After successful return of dup or dup2, the old and new descriptors may be used interchangeably.
dup uses the lowest-numbered unused descriptor for the new descriptor.
dup2 makes newfd be the copy of oldfd, closing newfd first if necessary.
EXAMPLES
Redirecting Standard Output to a File
The following example closes standard output for the current processes, reassigns standard output to go to the file referenced by pfd, and closes the o riginal file descriptor to clean up.
#include <unistd.h>
...
int pfd;
...
close(1);
dup(pfd);
close(pfd);
...
Redirecting Error Messages
The following example redirects messages from stderr to stdout.
#include <unistd.h>
...
dup2(1, 2);
...
#include <unistd.h>
int dup(int oldfd);
int dup2(int oldfd, int newfd);
dup and dup2 create a copy of the file descriptor oldfd.
After successful return of dup or dup2, the old and new descriptors may be used interchangeably.
dup uses the lowest-numbered unused descriptor for the new descriptor.
dup2 makes newfd be the copy of oldfd, closing newfd first if necessary.
EXAMPLES
Redirecting Standard Output to a File
The following example closes standard output for the current processes, reassigns standard output to go to the file referenced by pfd, and closes the o riginal file descriptor to clean up.
#include <unistd.h>
...
int pfd;
...
close(1);
dup(pfd);
close(pfd);
...
Redirecting Error Messages
The following example redirects messages from stderr to stdout.
#include <unistd.h>
...
dup2(1, 2);
...
相关阅读 更多 +