APUE Learning Example Source Code
guowenxue
2019-06-26 448a7c81fb0d7cda298c7faad8e407aa4cb70315
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h> 
#include <string.h> 
#include <errno.h> 
#include <unistd.h> 
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
 
int main(int argc, char *argv)
    int                 fd = -1; 
 
    fd = open("std.txt", O_RDWR|O_CREAT|O_TRUNC, 0666);
    if(fd < 0)
    {
        printf("Open file failure: %s\n", strerror(errno));
        return 1;
    } 
    
    dup2(fd, STDIN_FILENO);
    dup2(fd, STDOUT_FILENO);
    dup2(fd, STDERR_FILENO);
 
    printf("fd=%d\n", fd); 
    
    close(fd);
 
    return 0;
}