如何从其他程序运行的程序,并通过标准输入C或C ++数据传递给它? [英] How do i run a program from another program and pass data to it via stdin in c or c++?
问题描述
说,我有一个exe,可以说sum.exe。现在假设code为sum.exe是
Say i have an exe, lets say sum.exe. Now say the code for sum.exe is
void main ()
{
int a,b;
scanf ("%d%d", &a, &b);
printf ("%d", a+b);
}
我想知道我怎么可能会在别的C / C ++程序运行这个程序,并通过标准输入传递输入,就像他们在网上的编译器等网站ideone在那里我在一个文本框键入code,并提供标准输入数据做并且该数据是通过使用scanf或CIN程序接受。另外,我想知道是否有任何方式来阅读这个程序的自启动它的原始程序的输出。
I wanted to know how i could run this program from another c/c++ program and pass input via stdin like they do in online compiler sites like ideone where i type the code in and provide the stdin data in a textbox and that data is accepted by the program using scanf or cin. Also, i wanted to know if there was any way to read the output of this program from the original program that started it.
推荐答案
在C,主要组成部分是:
In C on platforms whose name end with X (i.e. not Windows), the key components are:
-
管道
- 返回一对文件描述符,所以什么写入到一个可以从另一个读
pipe
- Returns a pair of file descriptors, so that what's written to one can be read from the other.
叉
- 福克斯的过程中两个,既保持运行相同的code
fork
- Forks the process to two, both keep running the same code.
dup2
- 重新编排文件描述符。有了这个,你可以采取管的一端,并把它变成标准输入或标准输出。
dup2
- Renumbers file descriptors. With this, you can take one end of a pipe and turn it into stdin or stdout.
EXEC
- 停止运行当前的程序,开始运行另一个在同一进程
exec
- Stop running the current program, start running another, in the same process.
联合所有这些,你可以得到你问什么。
Combine them all, and you can get what you asked for.
这篇关于如何从其他程序运行的程序,并通过标准输入C或C ++数据传递给它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!