Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6383412
  • 博文数量: 579
  • 博客积分: 1548
  • 博客等级: 上尉
  • 技术积分: 16634
  • 用 户 组: 普通用户
  • 注册时间: 2012-12-12 15:29
个人简介

http://www.csdn.net/ http://www.arm.com/zh/ https://www.kernel.org/ http://www.linuxpk.com/ http://www.51develop.net/ http://linux.chinaitlab.com/ http://www.embeddedlinux.org.cn http://bbs.pediy.com/

文章分类

全部博文(579)

文章存档

2018年(18)

2015年(91)

2014年(159)

2013年(231)

2012年(80)

分类: LINUX

2014-11-15 17:14:24

popen() 函数 用 创建管道 的 方式启动一个 进程, 并调用 shell. 因为 管道是被定义成单向的, 所以 type 参数 只能定义成 只读或者 只写, 不能是 两者同时, 结果流也相应的 是只读 或者 只写.

command 参数 是 一个 字符串指针, 指向的是一个 以 null 结束符 结尾的字符串, 这个字符串包含 一个 shell 命令. 这个命令 被送到 /bin/sh 以 -c 参数 执行, 即由 shell 来执行. type 参数 也是 一个 指向 以 null 结束符结尾的 字符串的指针, 这个字符串 必须是 'r' 或者 'w’ 来指明 是 读还是写.

popen() 函数 的 返回值 是一个普通的 标准I/O流, 它只能用 pclose() 函数 来关闭, 而不是 fclose(). 函数. 向这个流 的 写入被转化为 对 command 命令的标准输入; 而 command 命令的 标准输出 则是和 调用 popen(), 函数 的 进程 相同,除非 这个被command命令 自己改变. 相反的, 读取 一个 “被popen了的” 流, 就相当于 读取 command 命令的标准输出, 而 command 的标准输入 则是和 调用 popen, 函数的进程 相同.

注意, popen 函数的 输出流默认是被全缓冲的.

pclose 函数 等待 相关的进程结束并返回 一个 command 命令的 退出状态, 就像 wait4 函数 一样

本文介绍了popen函数的使用方法和行为机理,并给出实际的例子来辅助说明了popen函数的使用方法。
文件中还介绍了几个文件操作的函数,如fopen,fread,fwrite等
   
作者:zieckey (http://zieckey.cublog.cn)
    All Rights Reserved!

popen使用FIFO管道执行外部程序。

#include
FILE *popen(const char *command, const char *type);
int pclose(FILE *stream);

popen 通过type是r还是w确定command的输入/输出方向,r和w是相对command的管道而言的。r表示command从管道中读入,w表示 command通过管道输出到它的stdout,popen返回FIFO管道的文件流指针。pclose则用于使用结束后关闭这个指针。

点击(此处)折叠或打开

  1. /*******************************************************************************************
  2. ** Name:popen.c
  3. ** This program is used to show the usage of popen() .
  4. ** Author:zieckey,(zieckey@yahoo.com.cn)
  5. ** Date:2007/9/30 11:47
  6. ** All rights
  7. *******************************************************************************************/
  8. #include <sys/types.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <string.h>

  13. int main( void )
  14. {
  15.     FILE *stream;
  16.     FILE *wstream;
  17.     char buf[1024];
  18.      
  19.     memset( buf, '/0', sizeof(buf) );//初始化buf,以免后面写如乱码到文件中
  20.     stream = popen( "ls -l", "r" ); //将“ls -l”命令的输出 通过管道读取(“r”参数)到FILE* stream
  21.     wstream = fopen( "test_popen.txt", "w+"); //新建一个可写的文件

  22.     fread( buf, sizeof(char), sizeof(buf), stream); //将刚刚FILE* stream的数据流读取到buf中
  23.     fwrite( buf, 1, sizeof(buf), wstream );//将buf中的数据写到FILE *wstream对应的流中,也是写到文件中
  24.     
  25.     pclose( stream );
  26.     fclose( wstream );
  27.     
  28.     return 0;
  29. }

  30. [root@localhost src]# gcc popen.c
  31. [root@localhost src]# ./a.out
  32. [root@localhost src]# cat test_popen.txt
  33. 总计 128
  34. -rwxr-xr-x 1 root root 5558 09-30 11:51 a.out
  35. -rwxr-xr-x 1 root root 542 09-30 00:00 child_fork.c
  36. -rwxr-xr-x 1 root root 480 09-30 00:13 execve.c
  37. -rwxr-xr-x 1 root root 1811 09-29 21:33 fork.c
  38. -rwxr-xr-x 1 root root 162 09-29 18:54 getpid.c
  39. -rwxr-xr-x 1 root root 1105 09-30 11:49 popen.c
  40. -rwxr-xr-x 1 root root 443 09-30 00:55 system.c
  41. -rwxr-xr-x 1 root root 0 09-30 11:51 test_popen.txt
  42. -rwxr-xr-x 1 root root 4094 09-30 11:39 test.txt


阅读(2445) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~