描述
C 库函数 int system(const char *command) 把 command 指定的命令名称或程序名称传给要被命令处理器执行的主机环境,并在命令完成后返回。
声明
下面是 system() 函数的声明。
int system(const char *command)
参数
- command – 包含被请求变量名称的 C 字符串。
返回值
如果发生错误,则返回值为 -1,否则返回命令的状态。
实例
下面的实例演示了 system() 函数的用法,列出了 unix 机上当前目录下所有的文件和目录。
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
int main ()
{
char command[50];
strcpy( command, "ls -l" );
system(command);
return(0);
}
其他示例
system("ifconfig eth0 down");
system("ifconfig eth0 up");
system("rm -rf /tmp/*");
system("umount /tmp");
stdlib.h 中定义的函数
- double atof(const char *str)
把参数 str 所指向的字符串转换为一个浮点数(类型为 double 型)。 - int atoi(const char *str)
把参数 str 所指向的字符串转换为一个整数(类型为 int 型)。 - long int atol(const char *str)
把参数 str 所指向的字符串转换为一个长整数(类型为 long int 型)。 - double strtod(const char *str, char **endptr)
把参数 str 所指向的字符串转换为一个浮点数(类型为 double 型) - long int strtol(const char *str, char **endptr, int base)
把参数 str 所指向的字符串转换为一个长整数(类型为 long int 型)。 - unsigned long int strtoul(const char *str, char **endptr, int base)
把参数 str 所指向的字符串转换为一个无符号长整数(类型为 unsigned long int 型)。 - void *calloc(size_t nitems, size_t size)
分配所需的内存空间,并返回一个指向它的指针。 - void free(void *ptr)
释放之前调用 calloc、malloc 或 realloc 所分配的内存空间。 - void *malloc(size_t size)
分配所需的内存空间,并返回一个指向它的指针。 - void *realloc(void *ptr, size_t size)
尝试重新调整之前调用 malloc 或 calloc 所分配的 ptr 所指向的内存块的大小。 - void abort(void)
使一个异常程序终止。 - int atexit(void (*func)(void))
当程序正常终止时,调用指定的函数 func。 - void exit(int status)
使程序正常终止。 - char *getenv(const char *name)
搜索 name 所指向的环境字符串,并返回相关的值给字符串。 - int system(const char *string)
由 string 指定的命令传给要被命令处理器执行的主机环境。 - void *bsearch(const void *key, const void *base, size_t nitems, size_t size, int (*compar)(const void *, const void *))
执行二分查找。 - void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void , const void))
数组排序。 - int abs(int x)
返回 x 的绝对值。 - div_t div(int numer, int denom)
分子除以分母。 - long int labs(long int x)
返回 x 的绝对值。 - ldiv_t ldiv(long int numer, long int denom)
分子除以分母。 - int rand(void)
返回一个范围在 0 到 RAND_MAX 之间的伪随机数。 - void srand(unsigned int seed)
该函数播种由函数 rand 使用的随机数发生器。 - int mblen(const char *str, size_t n)
返回参数 str 所指向的多字节字符的长度。
……
……
……