0

I have a simple C++ program compiled using command gcc 1.cpp -o 1.exe.

// 1.cpp    
#include <stdio.h>
int main(){
    int a = 0;
    scanf("%d", &a);
    printf("%d", a + 1000);
    scanf("%d", &a);
    printf("\n%d", a + 1000);
    return 0;
}

My goal is to create Node.js application which will be able to pass one number to 1.exe, then display result, and after that pass displayed result as next number. Finally, application must write second result and then close.
I've searched on Node.js manual's and documentations, and also on other sites, but I still cannot figure out how to create interactive child process in Node.js. The following code simply doesn't work.

var cp = require('child_process');
var proc = cp.spawn(pathToFile);
var n = 0;

proc.stdout.on('data', function(a){
    process.stdout.write(a);
    if(!n++) proc.stdin.write(a + '\n');
});

proc.stdin.write('1\n');

The function proc.stdout.on(data, ...) will never be reached, because it is called only when child process exit. Problem is because it will exit when second number is passed, but second number is passed when first result is received. I've tried many combinations and many options in cp.spawn function, but nothing worked as I expected.
The reason I want fully interactive child process is because I want to create Node.js module which use external application which requires a long time to load, so easier way is to keep child process active and just to send and receive data through stdio.
The C++ application is just an example. The real application I want Node.js to interact with is something I cannot edit, so do not expect from me to change the code of that application. I must adapt Node.js code to interact with application, not vice versa. When I execute external application with command prompt, I receive output after every input line, so it is not problem with that application.
Any ideas how to make fully interacting child process?

1 Answers1

0

Did you tried fflush?

 printf("%d", a + 1000);
 fflush(stdout);

ok you said that u can't change C code. but in that case I suggest you write another C program (broker.c) between node & 1.cpp. and invoke broker.cpp from node.

enRaiser
  • 2,606
  • 2
  • 21
  • 39
  • Did you read my question to the end? I said that C++ app is just an example. I have other application which I cannot modify or insert some code in it, because I don't have it's source code. –  Jul 16 '16 at 07:58
  • You again didn't read my question. That application is sending data (for example, if you execute it using command prompt, you can pass any number, and application immediately display result). The problem is because application requires tty to display it immediately, not pipe. I need a method to simulate tty inside Node.js, so application will behave like executed in command prompt. –  Jul 16 '16 at 08:02
  • can you do one thing. write another C programs to communicate with the said program. and if that works. then invoke your C program from node. – enRaiser Jul 16 '16 at 08:05
  • may be this link can help http://stackoverflow.com/questions/1401002/trick-an-application-into-thinking-its-stdin-is-interactive-not-a-pipe – enRaiser Jul 16 '16 at 08:10