您的位置:首页 > 编程语言 > C语言/C++

【c/c++】如何调用【Window】cmd命令行命令并获取命令行的输出内容

2017-04-20 20:34 721 查看
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int _System(const char * cmd, char *pRetMsg, int msg_len)
{
FILE * fp;
char * p = NULL;
int res = -1;
if (cmd == NULL || pRetMsg == NULL || msg_len < 0)
{
printf("Param Error!\n");
return -1;
}
if ((fp = _popen(cmd, "r")) == NULL)
{
printf("Popen Error!\n");
return -2;
}
else
{
memset(pRetMsg, 0, msg_len);
//get lastest result
while (fgets(pRetMsg, msg_len, fp) != NULL)
{
printf("Msg:%s", pRetMsg); //print all info
}

if ((res = _pclose(fp)) == -1)
{
printf("close popenerror!\n");
return -3;
}
pRetMsg[strlen(pRetMsg) - 1] = '\0';
return 0;
}
}

int main()
{
//test cmd
char *cmd = "python d:\\PythonProjects\\Demo1.py [1]";
char a8Result[128] = { 0 };
int ret = 0;
ret = _System(cmd, a8Result, sizeof(a8Result));
printf("ret = %d \na8Result = %s\nlength = %d \n", ret, a8Result, strlen(a8Result));
getchar();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐