Q: How do I run another program after a program runs? 1. The easiest way to run another program inside a program is to include them sequentially in a batch file (with the extension. BAT file), the programs listed in the batch file are run automatically when it is executed.
In either C or DOS, there is no specific way to do a function called "run another program after one program ends". However, C provides two sets of functions that allow one program to run another program at any time, and the latter run to end the former. If you put such a function call at the end of the first program, you can achieve the above goal. The two sets of functions provided by C are the exec () and spawn () function families, each of which has a function that distinguishes it from other functions. The EXEC () function family contains members such as EXECL (), Execle (), Execlpe (), Execv (), Execve (), and EXECVPE (). The following is a list of the meanings of the e,l,p and V suffixes in this:
E explicitly passes an array of pointers to the environment parameters to the child process
L PASS the command arguments to the program to be executed
P Find the function to execute through the environment variable path
V passes command-line arguments as an array of pointers to the program to be executed
Which function you choose in your program is entirely up to you and the needs of the program you want to execute. The program in the following example invokes another program whose parameters are specified by the command line:
#include <sio.h>
#include <process.h>
Char *envsing[]=
"Comm_vector=0x63",
"Parent=launch. EXE ",
"Exec=edit.com",
NULL};
void Main (int argc,char **argv)
{
_EXECVPE ("Edit.com", argv,envsing);
printf ("If You can read this sentence,the exec did ' NT happen!\n");
}
The short example above calls _EXECVPE () to execute the DOS file editor edit.com,edit the parameters of the program from the command line of the example. After calling the _EXECVPE function, the program in the example above ends when the edit program exits, and you return to the DOS prompt. If the print content of the printf statement appears on the screen, the _EXECVPE () function call has a problem, because if it succeeds, there is no such result. Note that the Edit.com environment variable provided by the above example does not make any sense. However, if the above example executes a program that requires an environment variable, then the supplied environment variable is available to the program.
Use the Spawn function to do the same work as the previous example. The Spwan () function family includes members such as SPAWNL (), Spawnle (), SPAWNLP (), Spawnlpe (), SPAWNV (), Spawnve (), SPAWNVP (), and spawn () functions. The meanings of the e,l,p in these function names are the same as those in the EXEC () function family function name. In fact, the spawn () function family is basically the same as the EXEC () function family, except that there is a small difference----the spawn () function can either start another program after the original program is finished, or start another program and return to the original program after the program ends. The parameters of the spawn () function are basically the same as the Exec () function, except that you need to add a parameter----you must use _p_overlay (end the original program) or _p_wait (return to the original program after end) as the first parameter of the spawn () function. The following example uses the Spawn () function to complete the same work as the previous example:
#include <sio.h>
#include <process.h>
Char *envsing[]={
COMM _vector=0x63 ",
" parent=launch. EXE,
exec=edit.com,
NULL};
void Main (int argc,char **argv)
{
_SPAWNVPE (_p_overlay, "Edit.com", argv,envsing);
printf ("If can read this sentence,the exec did ' NT happen!\n");
}
The only difference here is that "exec" changes to "spawn" and adds pattern (mode) parameters. The spawn () function has a cover and wait for two opposing functions, which allows you to make a decision whether to wait or to leave during the spawn () operation, and if you use _p_wait, wait.
2. There is also a way to run another program while a program is running, using the system () function. The system () function is similar to the preceding function but has a different place. In addition to suspending (rather than ending) the current program to execute a new program, System () also needs to start the Command.com command translator (or any other program that runs on your command translator). If system () cannot find the command command, it does not perform the required program (this is different from the exec () or spawn () function), and the following example calls Edit.com to edit a file:
#include <sio.h>
#include <process.h>
#include <slib.h>
Char args[256];
void Main (int argc,char **argv)
{
int ret;
sprintf (ArgS, "EDIT%s", (Argv[1]==null?) HELLO ": argv[1]));
Ret=system (ArgS);
printf ("System () returned%d\n", ret);
}
As in the example above (using _p_wait), the printf () statement following the system () call executes. Because the original program just hangs rather than terminates. In each case, system () returns a value indicating whether the specified program was successfully run, without returning the return value of the specified program.