简体   繁体   中英

Pass run time arguments to Nodejs child process

I want to compile C program using Nodejs child process.

C .out file execute function:

var exec= require('child_process').exec;
    exec("test.exe",function(err,stdout,stdin){
            //call back handling code here
    });

C program :

#include <stdio.h>
int main()
{
char msg[8];
 scanf("Please endter %s",&msg)
  printf("Hello world %s\n", msg);
  return 0;
}

How to pass runtime scanf input arguments to child process?

scanf() reads from stdin . Try these:

node script:

var exec = require('child_process').exec;
var cp = exec('test.exe', function(err, stdout, stderr) {
  process.stdout.write(stdout);
  process.stderr.write(stderr);
});
cp.stdin.end('node.js');

C program:

#include <stdio.h>
int main()
{
  char msg[8];
  scanf("%7s", msg);
  printf("Hello world %s\n", msg);
  return 0;
}

This outputs: Hello world node.js

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM