为什么说dup是完全违背 functional programming 的? 因为有 side effects。
commands and functions
a.out > file : put stdout into a file; a.out >& file: put stdout and stderr into a file; a.out < file : take stdin from a file; p1 | p2 : put stdout of p1 into the stdin of p2; p1 |& p2 : put stdout and stderr of p1 into the stdin of p2;int fd =fileno(fp); FILE *fp =fdopen(fd);// buffers a raw descriptorintpipe(int[]pipefd[2]);// 生成一个pipe,[0] 是read end, [1] 是write end, 相当于 // 将生成的管道两端存入 pipefd[]; dup(oldfd, newfd);// 将 oldfd 复制到 newfd,使得 newfd 指向同一个文件
open() is a low-level os call. fdopen() converts an os-level file descriptor to the higher-level FILE-abstraction of the C language. fopen() calls open() in the background and gives you a FILE-pointer directly.
1) fopen is a library function while open is a system call. 2) fopen provides buffered IO which is faster compare to open which is non buffered. 3) fopen is portable while open not portable (open is environment specific). 4) fopen returns a pointer to a FILE structure(FILE ); open returns an integer that identifies the file. 5) A FILE gives you the ability to use fscanf and other stdio functions.