Linux重定向的理解

 1 /*
 2     重定向的实例
 3     dup2函数
 4 
 5  利用filefd来代替STDOUT(标准输出流),write写入filefd的数据,重定向写出到STDOUT中;
 6 */
 7 
 8 #include <stdio.h>
 9 #include <sys/stat.h>
10 #include <string.h>
11 #include <fcntl.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 
15 int main(void)
16 {
17    #define STDOUT 1   //标准输出文件描述符号
18 
19    int filefd;
20    char msg[] = "This is a test
";
21 
22    filefd = open("dup.file", O_CREAT | O_RDWR,
23       S_IREAD | S_IWRITE);
24 
25      
26     dup2(STDOUT,filefd);
27 
28  
29     write(filefd,msg,strlen(msg));
30 
31     close(filefd);
32      close(STDOUT);
33     
34    return 0;
35 }
View Code

基本用处:模块A的输出作为模块B的输入,和函数的功能类似。

重定向的基本概念:http://blog.csdn.net/lyscsu/article/details/4636287

原文地址:https://www.cnblogs.com/xuxu8511/p/3174114.html