博客链接:
1 #include <stdio.h> 2 #include <unistd.h> 3 #include <sys/stat.h> 4 #include <fcntl.h> 5 6 7 int main(int argc, char* argv[]) 8 { 9 int fd = open("hello", O_CREAT|O_RDWR|O_TRUNC, S_IRUSR|S_IWUSR); 10 if(fd < 0) 11 { 12 printf("Open Error!! "); 13 return 0; 14 } 15 16 17 int nfd = dup(fd); 18 if(nfd < 0) 19 { 20 printf("Error!! "); 21 return 0; 22 } 23 24 25 char buf[1000]; 26 int n; 27 28 29 while((n = read(STDIN_FILENO, buf,1000)) > 0) 30 { 31 if(write(nfd, buf, n) != n) 32 { 33 printf("Write Error!! "); 34 return 0; 35 } 36 } 37 return 0; 38 }
输出结果: