我对这三个文件的用途感到相当困惑。如果我的理解是正确的,stdin是程序写入它的请求以在进程中运行任务的文件,stdout是内核写入它的输出和它访问的请求进程的信息的文件,stderr是所有异常都输入的文件。在打开这些文件来检查这些是否真的发生了,我发现似乎没有什么建议!
我想知道的是这些文件的确切目的是什么,绝对愚蠢的答案与很少的技术术语!
我对这三个文件的用途感到相当困惑。如果我的理解是正确的,stdin是程序写入它的请求以在进程中运行任务的文件,stdout是内核写入它的输出和它访问的请求进程的信息的文件,stderr是所有异常都输入的文件。在打开这些文件来检查这些是否真的发生了,我发现似乎没有什么建议!
我想知道的是这些文件的确切目的是什么,绝对愚蠢的答案与很少的技术术语!
当前回答
stdin
通过控制台读取输入(例如键盘输入)。 在C语言中使用scanf
scanf(<formatstring>,<pointer to storage> ...);
stdout
产生输出到控制台。 在C中与printf一起使用
printf(<string>, <values to print> ...);
stderr
向控制台输出“错误”。 在C中与fprintf一起使用
fprintf(stderr, <string>, <values to print> ...);
重定向
stdin的源可以被重定向。例如,它不是来自键盘输入,而是来自文件(echo < file.txt)或另一个程序(ps | grep <userid>)。
stdout、stderr的目的地也可以重定向。例如,stdout可以重定向到一个文件:ls。> ls-output.txt,在这种情况下输出被写入文件ls-output.txt。Stderr可以用2>重定向。
其他回答
stdin
通过控制台读取输入(例如键盘输入)。 在C语言中使用scanf
scanf(<formatstring>,<pointer to storage> ...);
stdout
产生输出到控制台。 在C中与printf一起使用
printf(<string>, <values to print> ...);
stderr
向控制台输出“错误”。 在C中与fprintf一起使用
fprintf(stderr, <string>, <values to print> ...);
重定向
stdin的源可以被重定向。例如,它不是来自键盘输入,而是来自文件(echo < file.txt)或另一个程序(ps | grep <userid>)。
stdout、stderr的目的地也可以重定向。例如,stdout可以重定向到一个文件:ls。> ls-output.txt,在这种情况下输出被写入文件ls-output.txt。Stderr可以用2>重定向。
stderr将不做IO缓存缓冲,所以如果我们的应用程序需要打印关键消息信息(一些错误,异常)到控制台或文件使用它作为使用标准输出打印一般日志信息,因为它使用IO缓存缓冲有一个机会,在写入我们的消息到文件应用程序可能会关闭,留下复杂的调试
有关这些文件的权威信息,请查看手册页,在您的终端上运行该命令。
$ man stdout
但简单来说,每个文件都是用于:
输出流的Stdout
流输入的Stdin
打印错误或日志消息的标准错误。
每个unix程序都有这些流中的每一个。
It would be more correct to say that stdin, stdout, and stderr are "I/O streams" rather than files. As you've noticed, these entities do not live in the filesystem. But the Unix philosophy, as far as I/O is concerned, is "everything is a file". In practice, that really means that you can use the same library functions and interfaces (printf, scanf, read, write, select, etc.) without worrying about whether the I/O stream is connected to a keyboard, a disk file, a socket, a pipe, or some other I/O abstraction.
大多数程序都需要读取输入,写入输出,并记录错误,所以stdin, stdout, 和stderr是为您预定义的,以方便编程。这只是 一种约定,操作系统不强制执行。
下面是一篇关于stdin, stdout和stderr的长篇文章:
Linux中的stdin、stdout和stderr是什么?
总结:
Streams Are Handled Like Files Streams in Linux—like almost everything else—are treated as though they were files. You can read text from a file, and you can write text into a file. Both of these actions involve a stream of data. So the concept of handling a stream of data as a file isn’t that much of a stretch. Each file associated with a process is allocated a unique number to identify it. This is known as the file descriptor. Whenever an action is required to be performed on a file, the file descriptor is used to identify the file. These values are always used for stdin, stdout, and stderr: 0: stdin 1: stdout 2: stderr
具有讽刺意味的是,我在堆栈溢出和上面的文章中发现了这个问题,因为我正在搜索关于异常/非标准流的信息。所以我的探索还在继续。