How to run c program in node js via child process in linux

Viewed 384

I am trying to create an application to run c/c++ programs using MEAN stack. I have an UI where programs can be written and node as backend where programs can be compiled via child process.

The basic c program with just printf option was executed properly with following commands.

program file name: test.c

#include<stdio.h>
int main(){
printf("hello world");
return 0;
}

Node commands : To compile the file, I used child process "exec("gcc -o test test.c", fun() { ... })" , It creates binary output file (test) without any extension.

To run that binary executed file, I used "exec("./test", fun (error, stdout, stderr) { ... }))" and it returns the output as "hello world".

But if I use "scanf" statement in program, for example:

#include<stdio.h>
int main(){
int a;
scanf("%d",&a);
printf("The number is", a);
return 0;
}

Here the command to run the program was not working. It just executes the program and i didn't get any output.

In this case, i want node child process to wait for user to enter "a" value from UI (i.e: angular) and returns the output.

Thank you in advance.

0 Answers
Related