下面是python脚本sup.py:
import subprocess
p = subprocess.Popen("test.exe", stdin = subprocess.PIPE,stdout = subprocess.PIPE, stderr = subprocess.PIPE, universal_newlines=True,shell =
False)
p.stdin.write('5\n')
p.stdin.write('3\n')
print(p.stdout.read())
下面是test.exe的C++源文件:
#include <iostream>
using namespace std;
int main(int argc, const char *artv[])
{
int x, y;
cout << "input x:"<< endl;
cin >> x;
cout << "input y:"<< endl;
cin >> y;
cout << x << " + " << y << " = " << x + y << endl;
return 0;
}
在windows的控制台中运行sup.py,结果如下图所示: