美文网首页
如何通过代码执行shell,并获得终端信息

如何通过代码执行shell,并获得终端信息

作者: 诸事圆成 | 来源:发表于2021-03-03 15:51 被阅读0次

FILE *popen(const char *command, const char *type);
fgets();

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

int execute_shell_command(const char *command, std::vector<std::string> & _hoge)
{
    char shell_buf[4096] = "";
    FILE *fp;
    fp = popen(command, "r");
    while(NULL != fgets(shell_buf, 4096, fp) )
    {
         if(shell_buf[strlen(shell_buf) - 1] == '\n')
              shell_buf[strlen(shell_buf)-1] = '\0';
         _hoge.push_back(shell_buf); //每次进来shell_buf都是新的,类似strcpy函数,上次存的数据,被新数据overwrite
     }
     plcose(fp)
     return _hoge.size();
     
}

相关文章

网友评论

      本文标题:如何通过代码执行shell,并获得终端信息

      本文链接:https://www.haomeiwen.com/subject/reedyctx.html