执行命令并获取结果函数

来源:互联网 发布:腾讯云和阿里云的比较 编辑:程序博客网 时间:2024/05/21 16:56

 

Windows下:获取cmd命令返回结果

 

 

 

linux下:

 

int systemex(const char* cmdstring, string &info){    if (cmdstring == NULL || cmdstring[0] == 0)        return EINVAL;    FILE *fd = popen(cmdstring, "r");    if (fd != NULL)    {            char buf[1024];        while (fgets(buf, 1024, fd))         {                info += buf;         }            pclose(fd);    }        else     {            Log(strerror(errno), "popen");        return errno;;    }        return 0;}