stdout 除了控制台窗口之外还有其他什么吗?
来自http://www.cplusplus.com/reference/iostream/cout/:
默认情况下,大多数系统将其标准输出设置为控制台,其中显示文本消息,尽管这通常可以重定向。
我从来没有听说过一个系统,其中 stdout
除了控制台窗口之外,无论默认情况还是其他情况。我可以看到重定向它在打印是一项昂贵操作的系统中可能是有益的,但这在现代计算机中不应该成为问题,对吗?
From http://www.cplusplus.com/reference/iostream/cout/:
By default, most systems have their standard output set to the console, where text messages are shown, although this can generally be redirected.
I've never heard of a system where stdout
is anything other than a console window, by default or otherwise. I can see how redirecting it might be beneficial in systems where printing is an expensive operation, but that shouldn't be an issue in modern computers, right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
当然可以。我可能想将标准输出重定向到文本文件、另一个进程、套接字等。
默认情况下它是控制台,但是有多种原因可以重定向它,最有用的(与 Unix 哲学一致)是将一个程序的输出重定向到另一个程序的输入。这使得人们能够创建许多小型、轻量级的程序,这些程序相互馈送并作为更大系统的离散部分工作。
基本上,它只是一个简单而强大的数据共享机制。由于我上面提到的原因,它在 *nix 系统上更流行,但它也适用于 Windows。
Of course it could be. I may want to redirect standard out to a text file, another process, a socket, whatever.
By default it is the Console, but the are a variety of reasons to redirect it, the most useful (in step with the Unix philosophy) being the redirection of the output of one program to the input of another program. This allows one to create many small, lightweight programs that feed into one another and work as discrete parts of a larger system.
Basically, it's just a simple yet powerful mechanism for sharing data. It is more popular on *nix systems for the reason I mention above, but it applies to Windows as well.
在大多数系统上,您可以将标准输入/输出/错误重定向到其他文件描述符或位置。
例如(在 Unix 上):
将 stdout 从 appname 重定向到名为 output 的文件。
将
stdout
重定向到名为output的文件,并将所有错误从stderr
重定向到名为errors的文件。在 UNIX 系统上,您还可以让程序打开文件描述符并将其指向
stdin
,如下所示:这将导致程序从管道中读取
stdin
。这就是在 UNIX 中如何将各种不同的实用程序“管道”在一起以创建一个更大的工具。
这将运行
find
命令,并获取其输出并将其通过管道传输到 ./appname,然后appname
的输出将发送到grep
的输入,然后搜索单词“search”,仅显示匹配的结果。它可以让许多小型实用程序发挥非常强大的作用。
将
>
、<
和|
想象成管道。>
就像水槽中的排水管,它接受数据并将其存储在您想要放置的地方。当 shell 遇到>
时,它将打开一个文件。当 shell 看到上述内容时,它将使用标准系统调用
打开
该文件,并记住该文件描述符。在上述情况下,由于没有输入,它将创建一个空文件并允许您输入更多命令。该命令将 Hello 用非常大的字母写入控制台,并使其滚动(我在这里使用 Unix,因为这是我最了解的)。输出只是写入标准输出。使用“接收器”(
>
)我们可以控制输出的位置,因此将导致横幅标准输出中的所有数据被重定向到 shell 已打开的文件描述符,从而被写入到名为
bannerout
的文件。管道的工作方式与
>
类似,它们有助于控制数据流向。然而,管道不能写入文件,只能用于帮助数据流从一个点到另一个点。例如,这里是流经多个变电站和废物清理的水:
水从湖中流出,通过管道流到水处理厂,从水处理厂回到泵,将其输送到水库,然后再次泵送进入市政水管并通过水槽进入玻璃杯。
请注意,管道只是将所有输出连接在一起,最终到达您的玻璃杯中。
这与在 Linux 上的 shell 中处理命令和处理它们的方式相同。它还遵循一条到达最终结果的路径。
现在还有最后一件事我在之前的陈述中尚未讨论,那就是
<
输入字符。它的作用是从文件中读取并将其输出到程序上的标准输入。将简单地打印出bannerout中存储的内容。如果您有一个要处理的文件,但由于不想在链中运行额外的命令而不想在前面添加
cat
,则可以使用此选项。因此,请尝试以下操作:
这将首先将字符串“Hello”放入文件
bannerinput
中,然后当您运行横幅时,它将从文件bannerinput
中读取。我希望这可以帮助您了解重定向和 pipping 在 Unix 上的工作原理(有些(如果不是大多数的话)也适用于 Windows)。
On most systems you can redirect the standard input/output/error to other file descriptors or locations.
For example (on Unix):
Redirects the stdout from appname to a file named output.
Redirects
stdout
to a file named output, and all errors fromstderr
to a file named errors.On unix systems you can also have a program open a file descriptor and point it at
stdin
, such as this:This will cause the program to read from the pipe for
stdin
.This is how in unix you can "pipe" various different utilities together to create one larger tool.
This will run the
find
command, and take its output and pipe it into ./appname, thenappname
's output will be sent togrep
's input which then searches for the word "search", displaying just the results that match.It allows many small utilities to have a very powerful effect.
Think of the
>
,<
, and|
like plumbing.>
is like the drain in a sink, it accepts data and stores it where you want to put it. When a shell encounters the>
it will open a file.When the shell sees the above, it will
open
the file using a standard system call, and remember that file descriptor. In the above case since there is no input it will create an empty file and allow you to type more commands.This command writes Hello in really big letters to the console, and will cause it to scroll (I am using Unix here since it is what I know best). The output is simply written to standard out. Using a "sink" (
>
) we can control where the output goes, sowill cause all of the data from banner's standard output to be redirected to the file descriptor the shell has opened and thus be written to a file named
bannerout
.Pipes work similarly to
>
's in that they help control the flow of where the data goes. Pipes however can't write to files, and can only be used to help the flow of data go from one point to another.For example, here is water flowing through several substations and waste cleaning:
The water flows from the lake, through a pipe to the water treatment plant, from the plant back into a pump that moves it to a reservoir, then it is pumped once more into the municipal water pipes and through your sink into your glass.
Notice that the pipes simply connect all of the outputs together, ultimately it ends up in your glass.
It is the same way with commands and processing them in a shell on Linux. It also follows a path to get to an end result.
Now there is one final thing that I hadn't discussed yet in my previous statements, that is the
<
input character. What it does is read from a file and output it to stdin on programs.Will simply print what was stored in bannerout. This can be used if you have a file you want to process, but don't want to prepend
cat <file>
because of not wanting to run an extra command in the chain.So try this:
This will first put the string "Hello" in the file
bannerinput
, and then when your run banner it will read from the filebannerinput
.I hope this helps you understand how redirection and pipping works on Unix (some if not most will apply to Windows as well).
到目前为止,所有答案都在调用该程序的事物(shell,无论什么)的上下文中。程序本身可以使
stdout
成为终端以外的东西。 C 标准库提供了 freopen,它允许程序员在任何兼容环境中重定向 stdout。 POSIX 提供了许多其他机制(popen
、fdopen
...),为程序员提供了更多控制权。我怀疑 Windows 提供了类似的机制。So far all of the answers have been in the context of the thing (shell, whatever) that invokes the program. The program itself can make
stdout
something other than the terminal. The C standard library providesfreopen
which lets the programmer redirect stdout in any compliant environment. POSIX provides a number of other mechanisms (popen
,fdopen
, ...) that gives the programmer even more control. I suspect Windows provides analogous mechanisms.三个标准文件描述符 0、1 和 2 可能发生任意数量的情况。任何人都可以使用附加到任何他们喜欢的文件描述符来启动新进程。
例如,GNU screen 将输出放入管道中并允许动态重新附加会话。 SSH 获取输出并将其返回到另一端。当然,所有众多的 shell 重定向器都会定期利用文件描述符进行操作。
Any number of things can happen to the three standard file descriptors 0, 1 and 2. Anyone can launch a new process with the file descriptors attached to anything they like.
For instance, GNU screen puts the output into a pipe and allows dynamic reattaching of a session. SSH takes the output and returns it to the other end. And of course all the numerous shell redirectors regularly make use of manipulating the file descriptors.
对于具有
stdout
的程序,它必须运行在托管实现(带有操作系统的实现)上,或者运行在带有附加功能的独立实现上。我很难在没有某种控制台的情况下实现这样的实现,但让我们假设火星漫游者有一个完整的操作系统,并且是用 C(或 C++)编程的,并且没有该控制台
可能已发送发送给 NASA 总部的消息。
For a program to have
stdout
it must be running on a hosted implementation (one with an Operating System), or on a free-standing implementation with extras.I'm having a hard time figuring such an implementation without a console of some kind, but let's suppose for a moment that Mars Rover has a full OS and is programmed in C (or C++) and doesn't have that console
might have sent the message to NASA headquarters.
如果您像这样运行程序,Windows 和 Linux 都会将
stdout
重定向到一个文件:my_program > some_file
这是最常见的情况,但许多其他类型的重定向也是可能的。在 Linux 上,您可以将
stdout
重定向到支持“文件描述符”接口的任何内容,例如管道、套接字、文件和各种其他内容。Both Windows and Linux will redirect
stdout
to a file if you run the program like this:my_program > some_file
This is the most common case, but many other types of redirection are possible. On Linux, you can redirect
stdout
to anything that supports the "file descriptor" interface, such as a pipe, socket, file, and various other things.一个可能想要重定向标准输出的简单例子是将信息传递给另一个程序时。 Unix/Linux 命令
ps
生成属于当前用户的进程列表。如果此列表很长并且您想要搜索特定进程,则可以输入将
ps
的stdout
重定向到grep 东西
。One simple example of a case where one might want to redirect stdout is when passing the information to another program. The Unix/Linux command
ps
generates a list of processes that belong to the current user. If this list was long and you wanted to search for a particular process, you could enterwhich would redirect the
stdout
ofps
to thestdin
ofgrep thing
.