获取system的打印信息

来源:互联网 发布:网络女歌手唱的谁不是 编辑:程序博客网 时间:2024/06/05 21:49
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;
}
阅读全文
0 0
原创粉丝点击