我已經使用下面的代碼將輸入傳遞給讀取輸入並打印它的cpp程序。如何通過python發送輸入到C++
from subprocess import Popen, PIPE
p = Popen(['a.exe'], shell=True, stdout=PIPE, stdin=PIPE)
for ii in range(10):
value = str(ii) + '\n'
value = bytes(value, 'UTF-8') # Needed in Python 3.
p.stdin.write(value)
#p.stdin.flush()
result = p.stdout.readline().strip()
print(result)
繼被蟒蛇
b''
b''
b''
b''
b''
b''
b''
b''
b''
b''
編輯的輸出: 以下是CPP文件
#include<iostream>
using namespace std;
int main()
{
int a[10];
for(int i=0;i<10;i++)
{
cin>>a[i];
cout<<a[i];
std::cout.flush();
}
return 0;
}
什麼是你的問題? –
a.exe是什麼?也許它返回空字符串。 – furas
輸出不正確。 – Sniper