如何通过代码执行shell,并获得终端信息
2021-03-03 本文已影响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();
}