博客链接:

 

 

 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!!\n");
13         return 0;
14     }
15 
16 
17     int nfd = dup(fd);
18     if(nfd < 0)
19     {
20         printf("Error!!\n");
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!!\n");
34             return 0;
35         }
36     }
37     return 0;
38 }

输出结果:

【APUE | 03】文件I/O

 

相关文章:

  • 2021-06-24
  • 2021-09-02
  • 2021-11-09
  • 2021-11-28
  • 2021-04-06
  • 2021-05-24
  • 2022-01-18
猜你喜欢
  • 2021-01-20
  • 2021-07-09
  • 2022-12-23
  • 2021-11-28
  • 2022-12-23
  • 2022-03-02
  • 2021-10-19
相关资源
相似解决方案