/****************************************************************/ /* Purpose: Trivial example illustrating */ /* 1. passing parameters from command line */ /* 2. invoking an executable from a C++ prog. */ /* Name: M. Overstreet */ /* Date: 25 Sept. 2005 */ /* Description: Reads and prints numbers until eof */ /****************************************************************/ /****************************************************************/ /* Listing Contents: */ /* Use instructions */ /* Reuse instructions */ /* Compilation instructions */ /* Source code */ /****************************************************************/ /****************************************************************/ /* Use Instructions */ /* Assumptions */ /* Program is compiled as described below */ /* Current directory is in user's path */ /* The program to be executed is in user's path and is */ /* "RunMe" */ /* The file the executed program is to read is "xyz" */ /* The file the executed program creates is to be "out" */ /* Then this program can be excuted by the command */ /* RunProg RunMe xyz out */ /****************************************************************/ /****************************************************************/ /* Reuse Instructions */ /* Program is so short, reuse opportunities are limited -- */ /****************************************************************/ /****************************************************************/ /* Compilation Instructions */ /* Compiled under Solaris with */ /* g++ -o RunProg RunProg.cpp */ /****************************************************************/ // Example of using system call. v. 1/27/05 // Still under development. // Purpose: illustrate running a program from another program // using the system call, system #include #include #include #include using namespace std; int main( int argc, char *argv[] ) { char CmdLine[50]; // Check input parameter count. if ( argc != 4 ) { cerr << "Not enough parameters. Program stopped." << endl; cerr << "Please enter the name of the program to be run, the name of the file it is to read as stdin, and the name of the output file it should create." << endl; return 1 ; } // Check to see if file exists ifstream InFile( argv[2] ); if ( !InFile ) { cerr << argv[2] << " not found." << endl; cerr << "Please enter the name of the program to be run, the name of the file it is to read as stdin, and the name of the output file it should create." << endl; return 1; } // Build string from command line params strcpy( CmdLine, argv[1] ); strcat( CmdLine, " < "); strcat( CmdLine, argv[2] ); strcat( CmdLine, " > "); strcat( CmdLine, argv[3] ); // Run string as command system( CmdLine ); return 0; }