popen()
来源:互联网 发布:华泰证券交易软件 mac 编辑:程序博客网 时间:2024/04/30 22:02
popen()函数原型如下:
FILE *popen(const char *cmd,const char *type);
返回值:若成功返回文件指针,出错则返回NULL
功能:创建一个管道,fork一个子进程,接着关闭管道的不使用端,子进程执行cmd指向的应用程序或者命令。
执行完该函数后父进程和子进程之间生成一条管道,函数返回值为FILE结构指针,该指针作为管道的一端,为父进程所拥有。子进程则拥有管道的另一端,该端口为子进程的stdin或者stdout。如果type=r,那么该管道的方向为:子进程的stdout到父进程的FILE指针;如果type=w,那么管道的方向为:父进程的FILE指针到子进程的stdin。
example:
主程序popen.c如下所示:
#include <unistd.h>#include <sys/types.h>#include <sys/wait.h>#include <errno.h>#include <stdio.h>#include <stdlib.h>int main(){char line[1024];FILE *fpin,*fpout;fpin=fopen("in.txt","r");fpout=popen("/my_fun","w");while(fgets(line,1024,fpin)!=NULL)fputs(line,fpout);pclose(fpout);fclose(fpin);return 0;}
my_fun.c程序代码如下:
#include <stdio.h>int main(){char line[1024];while(fgets(line,1024,stdin)!=NULL)fputs(line,stdout);return 0;}
程序分析如下:首先popen()函数创建一条管道,方向为父进程的fpout到子进程的stdin,接着popen程序打开in.txt文本并一行一行地读取出来写到管道的fpout端。子进程则从stdin中读取从父进程发送过来的数据显示到stdout中。
- popen()
- popen()
- popen()
- popen
- popen
- popen
- popen
- popen()
- popen 函数
- popen函数
- popen函数
- popen()函数
- os.popen
- 管道-popen
- popen函数
- 函数popen()
- Popen函数
- popen函数
- application/x-www-form-urlencoded multipart/form-data text/plain
- 连接时出错,(.ARM.exidx+0x0): undefined reference to `__aeabi_unwind_cpp_pr0'
- 做自己
- 解决ORACLE11g导出表不全问题
- Android学习笔记之AppWidget
- popen()
- 工作笔记
- 使用cle 调用opengl的简单例子(android)
- 校车
- LibGdx项目配置
- 15个值得开发人员关注的jQuery开发技巧和心得
- asp.net教程
- 插入排序
- win32 连接图片