如何使此管道在C ++中工作? [英] How to make this pipe work in c++?
问题描述
我正在用C ++编写Shell.它需要能够将输出从一件事传送到另一件事.例如,在Linux中,您可以通过执行cat textfile | more
将文本文件传输到更多文件.
I am programming a shell in c++. It needs to be able to pipe the output from one thing to another. For example, in linux, you can pipe a textfile to more by doing cat textfile | more
.
我将一个事物传递给另一个事物的函数是这样声明的:
My function to pipe one thing to another is declared like this:
void pipeinput(string input, string output);
我发送猫文本文件"作为输入,并发送更多"作为输出.
I send "cat textfile" as the input, and "more" as the output.
在显示如何制作管道的c ++示例中,使用了fopen().我作为输入发送到fopen()
会发送什么?我看过使用dup2
且不起诉dup2
的c ++管道示例. dup2的作用是什么?您怎么知道是否需要使用它?
In c++ examples that show how to make pipes, fopen() is used. What do I send as my input to fopen()
? I have seen c++ examples of pipeing using dup2
and without suing dup2
. What's dup2 used for? How do you know if you need to use it or not?
推荐答案
看看popen(3),这是避免execvp的一种方式.
Take a look at popen(3), which is a way to avoid execvp.
这篇关于如何使此管道在C ++中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!