Last active
August 30, 2022 18:48
-
-
Save likev/d1702d67235819b86f18a2a02ba5e329 to your computer and use it in GitHub Desktop.
Node.js IO pipes with c++ child_process
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { execFile } = require('child_process'); | |
const child = execFile('child-cpp'); | |
process.stdin.setEncoding('utf8'); | |
process.stdin.on('readable', () => { | |
const chunk = process.stdin.read(); | |
if (chunk !== null) { | |
//parent.stdin as child.stdin; | |
child.stdin.write(chunk); | |
} | |
}); | |
//child.stdout to parent stdout | |
child.stdout.on('data', (chunk) => { | |
process.stdout.write(chunk); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
int main() | |
{ | |
double result = 0, input = 0; | |
while(std::cout<<"input: " && std::cin >> input){ | |
if (input == 0) return 0; | |
result += input; | |
std::cout<<"result: "<<result<<std::endl; | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$> node parent.js | |
----------------- | |
input: 2 | |
result: 2 | |
input: 3 | |
result: 5 | |
input: 6 | |
result: 11 | |
input: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thx random ppl on the internet