I am using the npm library 'child_process' in an electron project to capture the stream of standard output from a C program with spawn. However, when I attempt to log the output from 'printf' or 'fprintf' captured by child.stdout.on('data'), nothing shows up. This is my test code:
#include <stdio.h>
#include <string.h>
int main(){
char buffer[256];
int i = 0;
while(1){
snprintf(buffer, sizeof(buffer), "echo %d\n", i);
printf("%s\n", buffer);
fprintf(stdout, "%s\n", buffer);
system(buffer);
system("sleep 1");
i++;
}
return 0;
}
I know that my javascript is correct because the line echo'ed by the system call is captured, and I have tested it with similar loops in a ruby script and a bash script and they have worked just fine.
When I run the binary file in a terminal shell everything outputs just fine, but when I try to capture all three outputs (first line should be "echo 0 \n echo 0 \n 0") with child.stdout.on('data', (data) => {...}); the only thing I get back is, like I said, the "0" from the system call. I am at a loss for the solution.
It might be notable that when I run this program in terminal, I can't use ^C to terminate it, as it is just picked up by stdin and carried up the screen with the output.
Here is my javascript if necessary:
const { spawn }= require('child_process');
let test;
// Invoked by a button that when clicked calls ipcRenderer.send('test');
ipcMain.on('test', (event, ...args) => {
test = spawn('./a.out', []);
test.stdout.on('data', (data) => {
console.log(data);
});
});