是标准输入文件- stdin
是总是键盘,标准输出文件- stdout
是总是> 屏幕,并且标准错误文件 - stderr
是始终屏幕?
为什么?
最佳答案
默认情况下,是的。 但系统之所以如此灵活和强大,是因为它可以重定向(由用户或程序两者)
当您在 shell 中输入时
command > file
您实际上将 command
的标准输出重定向到文件 file
中。
通过做
command1 | command2
将 command1
的标准输出重定向到 command2
的标准输入
以编程方式,file descriptor 0 始终为标准输入,1 为标准输出,2 为标准错误。
我建议了解 dup
and dup2
以编程方式重定向它们。
示例
int file = open("out.txt", O_APPEND | O_WRONLY);
int stdout_cpy = dup(1); // Clone stdout to a new descriptor
dup2(file, 1); // Make file the new fd 1, i.e. redirect stdout to out.txt
关于c - c 中的标准输出/输入/错误文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21041656/