c++里的system()函数调用系统命令如何获取打印信息
http://www.linuxidc.com/Linux/2011-04/34092.htm#include #include void executeCMD(const char *cmd, char *result){ char buf_ps[1024]; char ps[1024]={0}; FILE *ptr; strcpy
·
http://www.linuxidc.com/Linux/2011-04/34092.htm
#include <stdio.h>
#include <string>
void executeCMD(const char *cmd, char *result)
{
char buf_ps[1024];
char ps[1024]={0};
FILE *ptr;
strcpy(ps, cmd);
if((ptr=popen(ps, "r"))!=NULL)
{
while(fgets(buf_ps, 1024, ptr)!=NULL)
{
strcat(result, buf_ps);
if(strlen(result)>1024)
break;
}
pclose(ptr);
ptr = NULL;
}
else
{
printf("popen %s error\n", ps);
}
}
int main()
{
char result[1024];
executeCMD( "ls /opt/*.rpm | awk -F '[-|.]' '{print $3}'", result);
printf("%s", result );
return 0;
}
#include <stdio.h>
#include <string>
void executeCMD(const char *cmd, char *result)
{
char buf_ps[1024];
char ps[1024]={0};
FILE *ptr;
strcpy(ps, cmd);
if((ptr=popen(ps, "r"))!=NULL)
{
while(fgets(buf_ps, 1024, ptr)!=NULL)
{
strcat(result, buf_ps);
if(strlen(result)>1024)
break;
}
pclose(ptr);
ptr = NULL;
}
else
{
printf("popen %s error\n", ps);
}
}
int main()
{
char result[1024];
executeCMD( "ls /opt/*.rpm | awk -F '[-|.]' '{print $3}'", result);
printf("%s", result );
return 0;
}
更多推荐
已为社区贡献4条内容
所有评论(0)