#include
#define SIZE 100
int main(void)
{
int n;
char buf[SIZE];
while(n=read(STDIN_FILENO,buf,SIZE)) //读取标准输入到buf中,返回读取字节数。
{
if(n!=write(STDOUT_FILENO,buf,n)) 把buf 写到标准输出中
perror("write error");
}
if(n<0) perror("read error");
return 0;
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
如果按照常规的使用方法:
fan@fan:~/arm$ gcc -o app stdout.c
fan@fan:~/arm$ ./app
fdfe
fdfe
fefefef
fefefef
//输入一行,马上输出一行,也就是按照原样输出
===========================================================================
使用重定向来运行
fan@fan:~/arm$ ./app> data
fefe
fdadfdf
adfdfef
//输入的字符串并不是输出到终端,而是输出到data文件中。
==========================================================================
fan@fan:~/arm$ ./app data
fan@fan:~/arm$
//这一句相当于把stdout.c复制到data中去
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=
STDOUT_FILENO和STDIN_FILENO在默认的情况下是终端,
使用ctrl+D来输入文件结束符