免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1114 | 回复: 0
打印 上一主题 下一主题

popen函数调用 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-04-13 15:38 |只看该作者 |倒序浏览
#include
FILE *popen(const char *command, const char *type);
int pclose(FILE *stream);


说明:
popen使用FIFO管道执行外部程序。
popen() 函数 用 创建管道的 方式启动一个进程, 并调用 shell. 因为管道是被定义成单向的, 所以 type 参数只能定义成只读或者 只写, 不能是两者同时, 结果流也相应的 是只读或者只写.
参数:
popen 通过type是r还是w确定command的输入/输出方向,r和w是相对command的管道而言的。r表示command从管道中读入,w表示 command通过管道输出到它的stdout,popen返回FIFO管道的文件流指针。pclose则用于使用结束后关闭这个指针。
示例:
#include   
#include   
#include   
#include   
#include
int main( void )  
{  
    FILE   *stream;  
    char   buf[1024];
     
    memset( buf, '\0', sizeof(buf) );     /*初始化buf,以免后面写如乱码到文件中*/
    if((stream = popen("./mnt/env_lcd","r")) == NULL ); /*将“ls -l”命令的输出 通过
                                                 管道读取(“r”参数)到FILE* stream*/
    {
        printf("popen() error!\n");
        exit(1);
    }
    fread( buf, sizeof(char), sizeof(buf),  stream);
                                            /*将刚刚FILE* stream的数据流读取到buf中*/
    pclose( stream );     
    return 0;
}   
[/url]



[url=http://www.91linux.com/html/article/program/20071017/7637.html]




本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/77869/showart_1899375.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP