system函数声明为: int system(const char *string);
string为你要输入的命令。 实例如下:
/*
* Demonstrate the system() call
*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int retval;//retval is the return value of the system call
retval = system("ls -l");
if(retval == 127)
{
fprintf(stderr, "/bin/sh not available\n");
exit (127);
}
else if(retval == -1)
{
perror("system");
exit(EXIT_FAILURE);
}
else if(retval != 0)
{
fprintf(stderr, "command returned %d\n", retval);
perror("ls");
}
else
{
puts("command successfully executed");
}
return (0);
}