2

For a program that I want to write, I have to enter commands to and read from a console program, and I have no idea how to.

For understanding, perhaps a solution to the following example would be helpful:

I want to write a program that squares all natural numbers up to 100 and saves the result to a text file. Assume that I don't know how to square numbers and therefore use the executable file square_x.exe with the following (unavailable) source code:

#include <iostream>
using namespace std;

int main(){

    double in;

    cout << "Input: ";
    while(cin>>in){
        cout << in << static_cast<char>(253)/*²*/ << " = " << in*in << endl
             << endl
             << "Input: ";
    }

    return 0;
}

How then must I add to the following code fragment? (Proposedly, where indicated by "Somehow"):

#include <fstream>
using namespace std;

ofstream file;

void writeToFile( const char* name, const char* content ){
    file.open( name, ios::app );
    file << content;
    file.close();
}

int main(){

    char buffer[30], buffer2[50];

    //Somehow call square_x.exe

    for( int i = 1 ; i <= 100 ; i++ ){

        //Somehow send i to square_x.exe

        //Somehow get a result from square_x.exe and safe it to buffer2

        //Extract from the got string the result of the calculation
        strtok(buffer2," "); strtok(0, " ");
        strncpy(buffer2,strtok(0, " \n"),sizeof(buffer2)-1);

        sprintf( buffer , "%d\n%s\n\n" , i , buffer2 ); 

        writeToFile( "list.txt", buffer );
    }

    //Somehow close square_x.exe

    return 0;
}

I asked a friend of mine if he could help me with that and he sent me this. But since I have no idea how I would have to change this code to fulfill my needs, he then sent me here.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
abcbuck
  • 23
  • 6
  • What you're suggesting is waaaaay overkill for getting the square of a number. Why can't you just write a loop of 100 elements and store the square of the index in each element? The square is just the number multiplied by itself – Kanga_Roo Nov 18 '15 at 14:30
  • because not squaring the number is the problem of which I seek the solution – abcbuck Nov 18 '15 at 14:34
  • [_popen](https://msdn.microsoft.com/en-us/library/96ayss4b.aspx) might help. – Simple Nov 18 '15 at 14:46
  • 1
    @Simple: `popen` family functions are great for unidirectional streams, but here OP wants to pass information to child program and read its output. – Serge Ballesta Nov 18 '15 at 14:54
  • 1
    You might want to remove the winapi tag. Do you need more than one executable or just different functions? – David Nov 18 '15 at 14:58
  • Possible duplicate of [Read Output of an application](http://stackoverflow.com/q/20678635/1889329). – IInspectable Nov 18 '15 at 15:28

2 Answers2

3

I assume that want you really want is the way to pass input to an auxilliary program and get its output.

The referenced link explains how to do it using WinAPI functions. Unfortunately, there is no simpler solution on Windows, because of the lacking of a fork function(*).

Taking the link as reference you must replace in your source //Somehow call square_x.exe with pipes and child process creation (CreatePipeand CreateProcess API calls, child process here is square_x.exe). Then to send something to the child, you just use WriteFile on the write end of the input pipe, and to read from the child, you use ReadFile from the read end of the output pipe.

But you example will be a little harder, because you child adds the noisy string "Input: " that you will have to ignore.

Try to put this in your code, and come back here to ask another question (with the new code) if you are stuck.

(*) But you could find a library that deals with all that gory details for you, such as libexecstream proposed by @a486408.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
2

You can try using libexecstream - it allows you to launch new processes and communicate with them using c++ iostreams without digging into OS-specific stuff.

a486408
  • 68
  • 5
  • I did not know that library. It will certainly be simpler to use than WinAPI functions! Maybe it would deserve to be a little more elaborated with (for example) a typical use case. – Serge Ballesta Nov 18 '15 at 14:59