[英]Linux terminal pipe to my C++ program
I'm not sure about the terminology, and when I googled this I got many misleading results. 我不确定术语,当我用谷歌搜索时,我得到了许多误导性的结果。 I just need a push and I'll be fine.
我只需要一推就可以了。
How can put my program as a target to piping in linux terminal? 如何将我的程序作为Linux终端中管道传输的目标?
Say I wrote a C++ program MyProg 说我写了一个C ++程序MyProg
I would like to be able to do something like 我希望能够做类似的事情
$ ls | $ ls | ./MyProg
./MyProg
Could you please give me a hint to what function/stream can be used for this? 您能否给我一个提示,可以为此使用什么功能/流?
Piping, as you call it, is nothing special in Linux, that's why you do not find it. 正如您所称,管道在Linux中没什么特别的,这就是为什么找不到管道的原因。
What the pipe does is to redirect the standard output ( stdout
) of one program to the standard input ( stdin
) of another. 管道的作用是将一个程序的标准输出(
stdout
)重定向到另一个程序的标准输入( stdin
)。 So simply read the standard input, and you will find the text there. 因此,只需阅读标准输入,即可在此处找到文本。 You an use
std::cin
if you want to go the C++ way, or *scanf()
in C mode. 如果要使用C ++方式,请使用
std::cin
,或者在C模式下使用*scanf()
。
If you want to know if stdin
refers to the actual terminal or some redirection, you can use isatty(0)
(0 is the fd of stdin, there is a constant somewhere but I can't remember the name, while everybody know that stdin is 0). 如果您想知道
stdin
是指实际的终端还是某种重定向,可以使用isatty(0)
(0是stdin的fd,某个地方有一个常数,但我不记得它的名字了,而每个人都知道stdin是0)。
You should handle standard input (stdin) , output (stdout) and error (stderr) in your application. 您应该在应用程序中处理标准输入(stdin) ,输出(stdout)和错误(stderr) 。 They have reserved file descriptor numbers.
它们具有保留的文件描述符编号。 Please refer to following link File descriptor Wiki
请参考以下链接文件描述符Wiki
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.