/* 将标准输入复制到标准输出 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define BUFFSIZE 4096
void
err_sys(char *str)
{
perror(str);
exit(1);
}
int
main(int argc, char *argv[])
{
int n;
char buf[BUFFSIZE];
while ((n = read(STDIN_FILENO, buf, BUFFSIZE)) > 0) /* 将标准输入写到buf里 */
if (write(STDOUT_FILENO, buf, n) != n) /* 将buf里的内容写到标准输出 */
err_sys("write error");
if (n < 0) /* n为read函数的返回值,表示读取文件的长度 */
err_sys("read error");
exit(0);
}
/* "./1-2_stdin2out > infile" 将结果重定向到infile文件中
* "./1-2_stdin2out < infile > outfile" 将infile中的内容复制到outfile中*/