Skip to content

Instantly share code, notes, and snippets.

@jesyspa
Created February 14, 2015 23:45
Show Gist options
  • Save jesyspa/104ba5e58b7c9e31b1d8 to your computer and use it in GitHub Desktop.
Save jesyspa/104ba5e58b7c9e31b1d8 to your computer and use it in GitHub Desktop.
Example of pipes
#include <iostream>
int main() {
std::cerr << "[C++] Launched" << std::endl;
int total = 0;
int tmp;
while (std::cin >> tmp) {
std::cerr << "[C++] Read " << tmp << std::endl;
total += tmp;
}
std::cout << total << std::endl;
std::cerr << "[C++] Done" << std::endl;
}
import subprocess
import sys
print "[Python] Opened file"
proc = subprocess.Popen(["./test_program"], stdout = subprocess.PIPE, stdin = subprocess.PIPE, stderr=sys.stderr)
for i in range(5):
proc.stdin.write(str(i) + '\n')
proc.stdin.close()
print "[Python] Done writing!"
for line in iter(proc.stdout.readline, ''):
print '[Python] Got:', repr(line)
print "[Python] Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment