popen使用FIFO管道执行外部程序
来源:互联网 发布:linux finger 编辑:程序博客网 时间:2024/05/31 13:14
摘要:本文介绍了popen函数的使用方法和行为机理,并给出实际的例子来辅助说明了popen函数的使用方法。
popen 通过type是r还是w确定command的输入/输出方向,r和w是相对command的管道而言的。r表示command从管道中读入,w表示 command通过管道输出到它的stdout,popen返回FIFO管道的文件流指针。pclose则用于使用结束后关闭这个指针。
下面看一个例子:
#include <stdio.h>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则用于使用结束后关闭这个指针。
下面看一个例子:
/********************************************************************************************* Name:popen.c** This program is used to show the usage of popen() .** Author:zieckey,(zieckey@yahoo.com.cn)** Date:2007/9/30 11:47** All rights reserved!*******************************************************************************************/#include <sys/types.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h>int main( void ) { FILE *stream; FILE *wstream; char buf[1024]; memset( buf, '\0', sizeof(buf) );//初始化buf,以免后面写如乱码到文件中 stream = popen( "ls -l", "r" ); //将“ls -l”命令的输出 通过管道读取(“r”参数)到FILE* stream wstream = fopen( "test_popen.txt", "w+"); //新建一个可写的文件 fread( buf, sizeof(char), sizeof(buf), stream); //将刚刚FILE* stream的数据流读取到buf中 fwrite( buf, 1, sizeof(buf), wstream );//将buf中的数据写到FILE *wstream对应的流中,也是写到文件中 pclose( stream ); fclose( wstream ); return 0;}
[root@localhost src]# gcc popen.c[root@localhost src]# ./a.out [root@localhost src]# cat test_popen.txt总计 128-rwxr-xr-x 1 root root 5558 09-30 11:51 a.out-rwxr-xr-x 1 root root 542 09-30 00:00 child_fork.c-rwxr-xr-x 1 root root 480 09-30 00:13 execve.c-rwxr-xr-x 1 root root 1811 09-29 21:33 fork.c-rwxr-xr-x 1 root root 162 09-29 18:54 getpid.c-rwxr-xr-x 1 root root 1105 09-30 11:49 popen.c-rwxr-xr-x 1 root root 443 09-30 00:55 system.c-rwxr-xr-x 1 root root 0 09-30 11:51 test_popen.txt-rwxr-xr-x 1 root root 4094 09-30 11:39 test.txt
0 0
- popen使用FIFO管道执行外部程序
- FIFO 有名管道的使用
- 有名管道FIFO的使用
- linux c之通过popen和pclose函数创建管道执行shell 运行命令使用总结
- 管道-popen
- 管道popen
- popen管道
- 管道popen
- 使用popen函数创建ping命令管道
- 命名管道(FIFO)聊天程序
- C语言popen创建管道文件循环执行shell命令
- FIFO管道
- 管道-fifo
- 管道,fifo
- VC中利用管道技术取得DOS或者命令行以及外部程序的执行结果
- VC中利用管道技术取得DOS或者命令行以及外部程序的执行结果
- 关于Python中并发调用管道执行外部程序的问题解决
- VC中利用管道技术取得DOS或者命令行以及外部程序的执行结果
- leetcode_Sort a linked list in O(n log n) time using constant space complexity.
- libc++abi.dylib: terminate_handler unexpectedly threw an exception错误解决方案
- struts.xml 使用 type="redirect" 和type="redirectaction" 传递参数
- System V 信号量
- xcode更新到5.1,"Cast from pointer to smaller type 'int' loses information” in EAGLView.mm
- popen使用FIFO管道执行外部程序
- 一个程序员如何快速赚到一百万?
- store.js - 轻松实现本地存储(LocalStorage)
- Server 2003安装IIS
- 千万不要把 bool 设计成函数参数(酷壳)
- 链表逆序、判断是否有环、求环的起点;两个链表是否相交、交点
- 2014年全球16大游戏引擎
- 一致性测试--总结_2
- c++下bool值未初始化的问题