铅笔

在你的害怕中坚持的越多,你就会越自信
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

linux中常用的系统函数

Posted on 2020-10-11 10:14  黑色の铅笔  阅读(631)  评论(0编辑  收藏  举报

1.system()函数

头文件 
 #include <stdlib.h>

函数定义
 int system(const char * string); 

函数说明:通过linux命令 man 3 system 可以看到该函数的作用是:执行shell命令

system()会调用fork()产生子进程,由子进程来调用/bin/sh-c string来执行参数string字符串所代表的命令。此命
令执行完后随即返回原调用的进程。在调用system()期间SIGCHLD 信号会被暂时搁置,SIGINT和SIGQUIT 信
号则会被忽略。

实例说明:

比如需要将一些文件拷贝到另一个目录

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

int main(int argc,char* argv[])
{
    std::string cp_txt_commond = "/bin/cp -f ";//-f:覆盖已经存在的目标文件而不给出提示
    cp_txt_commond += "*.txt ";
    cp_txt_commond += "dir/";
    
    printf("cp_txt_commond result :%d" ,system(cp_txt_commond.c_str() ));
    return 0;
}