参考博客园博客:https://www.cnblogs.com/alantu2018/p/8554281.html
int my_system(const char cmd)
{
FILE fp;
int res;
char buf[1024];
std::string strError;
if (cmd == NULL)
{
//printf("my_system cmd is NULL!\n");
strError = "my_system cmd is NULL!\n";
return -1;
}
if ((fp = _popen(cmd, "r")) == NULL)
{
perror("popen");
//printf("popen error: %s/n", strerror(errno));
strError = strerror(errno);
return -1;
}
else
{
while (fgets(buf, sizeof(buf), fp))
{
//printf("%s", buf);
strError += buf;
}
if ((res = _pclose(fp)) == -1)
{
//printf("close popen file pointer fp error!\n");
strError = "close popen file pointer fp error!\n";
return res;
}
else if (res == 0)
{
return res;
}
else
{
//printf("popen res is :%d\n", res);
strError = "popen res is" + to_string((long long)res);
return res;
}
}
}
|