Calling other programs
While the typical way to use a program
that reads from standard input and writes to standard output is within a Unix
shell script or DOS batch file, any program can be called from inside a C or C++
program using the Standard C system( )
function, which is declared in the header file
<cstdlib>:
//: C02:CallHello.cpp
// Call another program
#include <cstdlib> // Declare "system()"
using namespace std;
int main() {
system("Hello");
} ///:~
To use the system( )
function, you give it a character array that you would normally type at the
operating system command prompt. This can also include command-line arguments,
and the character array can be one that you fabricate at run time (instead of
just using a static character array as shown above). The command executes and
control returns to the program.
This program shows you how easy it is to
use plain C library functions in C++; just include the header file and call the
function. This upward compatibility from C to C++ is a
big advantage if you are learning the language starting from a background in
C.