Linux Run Shell in C++
int cmd_line(const char *cmd, string& strRet) {
FILE *stream;
int readlen;
strRet.clear();
char s[2048];
stream = popen(cmd, "r");
if (stream)
{
while (fgets(s, 2048, stream))
strRet += s;
}
pclose(stream);
return readlen;
}
int readAllText(const char* path, string& strRet) {
ifstream fin(path);
if (!fin.is_open()) {
cout << "open failed!\n";
return -1;
}
strRet.clear();
char ch;
while (fin.get(ch)) {
strRet += ch;
}
fin.close();
return strRet.size();
}
int run_shell(const char* shell_path, string& strret)
{
string strcmd;
int iret = readAllText(shell_path, strcmd);
if (iret > 0)
{
cmd_line(strcmd.c_str(), strret);
}
return iret;
}