I am trying to create a python script that will send lines into a cpp file running on a while loop and printing the lines received into the console.
test.py
#test.py
import subprocess
p = subprocess.Popen('./stdin.out',bufsize=1,stdin=subprocess.PIPE,stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, universal_newlines=True)
p.stdin.write('hello world\n')
p.stdin.write('hell world\n')
p.stdin.write('hel world\n')
p.terminate()
stdin.cpp
//stdin.cpp
#include <iostream>
int main(){
std::string line;
std::cout << "Hello World! from C++" << std::endl;
while (getline(std::cin, line)) {
std::cout <<"Received from python:" << line << std::endl;
}
return 0;
}
Is this achievable?
p.stdin.close()follwed byp.wait().