在Node.js中,怎么向spawn和exec产生的子进程的stdin输入东西??
发布于 11 年前 作者 friskit-china 20516 次预览 最后一次回复是 11 年前 来自
例如程序:
#include <stdio.h>
int main(){
int a,b;
while(scanf("%d %d", &a, &b)!=EOF)
printf("%d\n",a+b);
return 0;
}
然后通过
var exec = require('child_process').exec;
var childProcess = exec('a.out',
function(err, stdout, stderr){
//balabalabala
//我想向a.out的stdio中输入东西需要怎么做啊……
}
);
5 回复
这样试试:
执行后,在控制台观察输出。
采用child_process的exec方法可以这样来写:
可以试着把最后两行pipe函数去掉,看看有什么结果 :)
exec也有stdin这些数据成员??这个主要是我疑惑的地方……
感觉就是通过重定向,把子进程包在了父进程中……等手边有环境了试试哈~多谢~
@friskit-china 别疑惑了。官网文档http://nodejs.org/api/child_process.html,
这4个函数都返回ChildProcess对象。 Class ChildProcess也在那个网页上有介绍。