I have got a c program,which reads integer values from stdin. I wrote a nodejs program to execute the c file, and the nodejs program will read a text file(containg numbers in multiple lines) and pipe this data to stdin of the child process.
The problem is, if the no of inputs in the txt file is less than the expected number then the child process will be supplied with value 0. I want the child process to wait until the data is received.
c program
#include<stdio.h>
int main(){
int a;
printf("hekllowworls");
scanf("%d",&a);
printf("%d",a);
scanf("%d",&a);
printf("%d",a);
scanf("%d",&a);
printf("%d",a);
}
Node JS Program
var fs = require('fs'),
cp = require('child_process');
stream = fs.createReadStream('myfile.txt');
var obj = cp.spawn('/home/arju/Desktop/exec/a.out');
stream.pipe(obj.stdin);
obj.stdout.on('data',function(data){
console.log(data.toString());
});
obj.on('error',function(err){
console.log(err);
});
TextFile - Myfile.txt
10
20