abcbuck
abcbuck

Reputation: 23

How to enter to and read from a console program?

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.

Upvotes: 2

Views: 191

Answers (2)

a486408
a486408

Reputation: 78

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.

Upvotes: 3

Serge Ballesta
Serge Ballesta

Reputation: 149155

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.

Upvotes: 4

Related Questions