除了控制台窗口外,stdout还没有其他功能吗? [英] Is stdout Ever Anything Other Than a Console Window?

查看:86
本文介绍了除了控制台窗口外,stdout还没有其他功能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 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.

我从未听说过 stdout 是什么的系统默认情况下,控制台窗口除外。我可以看到重定向对于在打印操作很昂贵的系统中可能有什么好处,但这在现代计算机中不应该成为问题,对吧?

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?

推荐答案

在大多数系统上,您可以将标准输入/输出/错误重定向到其他文件描述符或位置。

On most systems you can redirect the standard input/output/error to other file descriptors or locations.

例如(在Unix上):

For example (on Unix):

./appname > output

将标准输出从appname重定向到名为output的文件。

Redirects the stdout from appname to a file named output.

./appname 2> errors > output

stdout 重定向到名为output的文件,以及从 stderr 到错误文件的所有错误。

Redirects stdout to a file named output, and all errors from stderr to a file named errors.

在Unix系统上,您还可以打开一个程序文件描述符,并将其指向 stdin ,例如:

On unix systems you can also have a program open a file descriptor and point it at stdin, such as this:

echo "input" > input
cat input | ./appname

这将导致程序从管道中读取 stdin

This will cause the program to read from the pipe for stdin.

这是在UNIX中将各种不同的实用程序管道在一起创建的方式一个更大的工具。

This is how in unix you can "pipe" various different utilities together to create one larger tool.

find . -type f | ./appname | grep -iv "search"

这将运行查找命令,并获取其输出并将其通过管道传输到./appname,然后 appname 的输出将发送到 grep 的输入,然后搜索单词 search,仅显示匹配的结果。

This will run the find command, and take its output and pipe it into ./appname, then appname's output will be sent to grep'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.

想想> < | 就像管道。

> 就像接收器中的流失,它接受数据并将其存储在要放置的位置。当外壳程序遇到> 时,它将打开一个文件。

> 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.

> file

当shell看到以上内容时,它将打开使用标准系统调用的文件,并记住该文件描述符。在上述情况下,由于没有输入,它将创建一个空文件并允许您键入更多命令。

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.

banner Hello

此命令以非常大的字母将Hello写入控制台,并导致其滚动(我正在使用Unix,因为这是我最了解的)。输出只需写入标准输出即可。使用接收器(> ),我们可以控制输出的位置,因此

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, so

banner Hello > bannerout

将导致banner的标准输出中的所有数据都重定向到外壳具有的文件描述符打开,并因此将其写入名为 bannerout 的文件。

will 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:

pump --from lake | treatment --cleanse-water | pump | reservoir | pump > glass

水从湖中流过,通过管道流到水处理厂,从工厂流回注入到将其移动到水库的泵中,然后再将其泵入市政水管,再通过水槽注入玻璃。

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.

命令和在Linux上的shell中处理命令的方式相同。

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.

现在,在前面的语句中我还没有讨论过最后一件事,那就是< 输入字符。从文件中读取它的作用,并将其输出到程序的stdin中。

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.

cat < bannerout

只需打印存储在bannerout中的内容。如果您有要处理的文件,但又不想在 cat< file> 之前加上它,则可以使用此命令。

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:

echo "Hello" > bannerinput
banner < bannerinput

这首先将字符串 Hello放入文件 bannerinput ,然后在您的运行标题时,它将从文件 bannerinput 中读取。

This will first put the string "Hello" in the file bannerinput, and then when your run banner it will read from the file bannerinput.

I希望这可以帮助您了解Unix上重定向和插入的工作方式(某些(即使不是大多数)也适用于Windows)。

I hope this helps you understand how redirection and pipping works on Unix (some if not most will apply to Windows as well).

这篇关于除了控制台窗口外,stdout还没有其他功能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆