Chinaunix首页 | 论坛 | 博客
  • 博客访问: 34398
  • 博文数量: 13
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 140
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-30 15:10
文章分类

全部博文(13)

文章存档

2009年(13)

我的朋友

分类: LINUX

2009-04-13 15:38:55

#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;
}  
 
 
 
 
阅读(858) | 评论(1) | 转发(0) |
0

上一篇:没有了

下一篇:"tty"终端设备解释

给主人留下些什么吧!~~

chinaunix网友2010-02-05 19:16:42

我的小站: http://www.wangchenxicool.yayapage.com/ ~嵌入式/linux/arm/pic学习、交流 ... .... [嵌入式产品设计、制作] ~ ----------来坐坐吧!^_^ (=^ ^=)