使用system函数

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);
}




posted @ 2012-10-26 17:38  CodingMonkey  阅读(179)  评论(0编辑  收藏  举报