2

lets say we have a cal.exe file (a simple addition calculator programmed programmed in c++). lets say that the console output screen first displays enter the first number: and waits for the user to input an integer value. I am willing to create a c++ program that would "pass on" the required value to the running process (cal.exe) as an input (playing the role of a user). I would also like to have the output from the cal.exe file to be displayed and interpreted by my program.

I havent got the slightest idea how to proceed with this. Is there any open source library that would help me accomplish this? If there is, could you name a few?

I have just learned object oriented programming in c++ last year in my school and I am not used to these kind of stuff in programming; so please excuse me if this question is silly.

update:

lets consider 2 processes a.exe and b.exe running. could you tell me a possible way to program b.exe which provides a integer input to a.exe (a console process) as if it was from the user?

Community
  • 1
  • 1
Sreram
  • 491
  • 1
  • 9
  • 22
  • You can read a console input using [`cin`](http://stackoverflow.com/questions/13096719/read-input-numbers-separated-by-spaces). To print on the console, you can use `cout` for example. – wendelbsilva Jul 30 '14 at 17:03
  • excuse me for that spelling mistake – Sreram Jul 30 '14 at 17:08
  • 1
    Oh, you mean [Inter-process communication](http://en.wikipedia.org/wiki/Inter-process_communication#Main_IPC_methods)? There's also this [answer here](http://stackoverflow.com/questions/372198/best-way-for-interprocess-communication-in-c). – wendelbsilva Jul 30 '14 at 17:09
  • yes; could you refer any library file I could use to achieve this? – Sreram Jul 30 '14 at 17:10
  • @user3633270 For a library, check the second link in my previous comment. To know more about all the options, check the first link. – wendelbsilva Jul 30 '14 at 17:12

1 Answers1

1

You can do this by accepting command line arguments.something like this

int main ( int argc, char *argv[] ) {

enter code here
return 0;

}

Where, First argument to main function (argc) refers to the number of arguments being passed to the program at run-time. Second (char *argv[] )refers to a string containing the arguments that are passed (char * is treated as String also ).

Argument names may vary as per the user specifications.

For details Refer: http://www.cplusplus.com/articles/DEN36Up4/

And for nesting of programs you can use system("name of child program goes here") Function under stdlib.h.

For details Refer: http://www.cplusplus.com/reference/cstdlib/system/

OshoParth
  • 1,492
  • 2
  • 20
  • 44
  • This only works when one executable launches another, and all data must be passed when the second executable is launched. – Thomas Matthews Jul 30 '14 at 19:07
  • You may want to search for "linux expect utility." It is a standalone program with a scripting language used for these kinds of problems. – NicholasM Jul 31 '14 at 02:59