popen在程序内调用可执行程序

时间:2015-01-13 16:02:52   收藏:0   阅读:268

一般有3种方法:1.exec函数 2.system函数 3. popen

int xpipe(char *cmdname, char *outbuf, int outlen)
{
    int ret = 0;
    FILE *pf;

    pf = popen(cmdname, "r");
    if( !pf )
        return -1; 

    if(outbuf)
    {   
        ret = fread(outbuf, 1, outlen-1, pf);
        if(ret<=0)
        {   
            pclose(pf);
            return ret;
        }   

        while( outbuf[ret-1] == '\r' || outbuf[ret-1] == '\n' )
            ret--;
        outbuf[ret] = 0;
    }   

    pclose(pf);
    return ret;
}



原文:http://blog.csdn.net/aa838260772/article/details/42676751

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!