Skip to content

Instantly share code, notes, and snippets.

@likev
Last active August 30, 2022 18:48
Show Gist options
  • Select an option

  • Save likev/d1702d67235819b86f18a2a02ba5e329 to your computer and use it in GitHub Desktop.

Select an option

Save likev/d1702d67235819b86f18a2a02ba5e329 to your computer and use it in GitHub Desktop.
Node.js IO pipes with c++ child_process
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);
});
#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;
}
$> node parent.js
-----------------
input: 2
result: 2
input: 3
result: 5
input: 6
result: 11
input:
Copy link
Copy Markdown

ghost commented Dec 10, 2021

thx random ppl on the internet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment