I have a program p1.exe that is continuously generating data (assume prime numbers) and prints in the console. I need to run a node program, that runs p1.exe inside it and we need to read all the data(here prime numbers) continuously, add 1 to each of it and print to the console in the Node JS program. This is not a homework problem.
Asked
Active
Viewed 500 times
0
-
Homework or not, we want your attempt! – Teemu Nov 06 '17 at 05:39
-
Take a look at this answer https://stackoverflow.com/a/20643568/1552587 – Titus Nov 06 '17 at 05:41
1 Answers
2
This should work, you can understand more from https://nodejs.org/api/child_process.html
const { spawn } = require('child_process');
function runAndGetPrimes(cb) {
var exe = spawn("p1.exe"),
output = "";
exe.stdout.on('data', (data) => {
output += data.toString();
});
exe.stderr.on('data', (data) => { });
exe.on('close', (code) => {
cb(output.split(" "));
});
}
runAndGetPrimes((primes) => {
primes.forEach((p) => {
console.log(parseInt(p, 10) + 1);
});
});