NAME popen, pclose - pipe stream to or from a PRocessSYNOPSIS #include <stdio.h> FILE *popen(const char *command, const char *type); int pclose(FILE *stream);DESCRIPTION The popen() function opens a process by creating a pipe, forking, and invoking the shell. Since a pipe is by definition unidirectional, the type argument may specify only reading or writing, not both; the resulting stream is correspondingly read-only or write-only. The command argument is a pointer to a null-terminated string contain- ing a shell command line. This command is passed to /bin/sh using the -c flag; interpretation, if any, is performed by the shell. RETURN VALUE The popen() function returns NULL if the fork(2) or pipe(2) calls fail, or if it cannot allocate memory. The pclose() function returns -1 if wait4(2) returns an error, or some other error is detected.寫
popen.c,如下:
/************************************************************************* > File Name: popen.c > Author: KrisChou > Mail:zhoujx0219@163.com > Created Time: Fri 22 Aug 2014 11:07:26 AM CST ************************************************************************/#include <stdio.h>#include <stdlib.h>#include <string.h>int main(int argc, char *argv[]){ char buf[1024]; FILE *fp; while(memset(buf,0,1024),fgets(buf,1024,stdin) != NULL) { fp = popen(argv[1],"w"); fputs(buf,fp); pclose(fp); } return 0;}
被調用函數reverse.c,如下:
/************************************************************************* > File Name: reverse.c > Author: KrisChou > Mail:zhoujx0219@163.com > Created Time: Sat 23 Aug 2014 11:21:27 AM CST ************************************************************************/#include <stdio.h>#include <stdlib.h>#include <string.h>int main(int argc, char *argv[]){ char Word[256]; //從標準輸入讀取字符串 char buf[256][256],index = 0; //保存從標準輸入讀取的字符串 while(memset(word,0,256), scanf("%s",word) != EOF ) { strcpy(buf[index++],word); } index--; while(index >=0 ) { printf("%s ",buf[index]); index--; } printf("/n"); return 0;}
運行程序:
[purple@localhost popen]$ gcc popen.c -o main[purple@localhost popen]$ gcc reverse.c -o reverse[purple@localhost popen]$ ./main ./reversehow are youyou are howbaby u r beautifulbeautiful r u baby[purple@localhost popen]$
按 ctrl+D 退出popen.c中的循環,從而退出程序。
讀popen.c,如下:
/************************************************************************* > File Name: popen.c > Author: KrisChou > Mail:zhoujx0219@163.com > Created Time: Sun 24 Aug 2014 08:53:14 AM CST ************************************************************************/#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>int main(int argc, char *argv[]){ FILE *fp; /* 標準管道描述符 */ char buf[1024]; /* 存放所要調用程序的參數 */ char cmd[1024]; /* 存放所要調用的程序的命令行 */ while(memset(buf,0,1024),fgets(buf,1024,stdin)) { sprintf(cmd,"%s %s",argv[1],buf); fp = popen(cmd,"r"); memset(buf,0,1024); fgets(buf,1024,fp); puts(buf); pclose(fp); } return 0;}
被調用函數reverse.c,如下:
/************************************************************************* > File Name: reverse.c > Author: KrisChou > Mail:zhoujx0219@163.com > Created Time: Sun 24 Aug 2014 09:03:37 AM CST ************************************************************************/#include <stdio.h>#include <stdlib.h>#include <string.h>int main(int argc, char *argv[]){ int index; for(index = argc - 1; index > 0 ; index--) { printf("%s ",argv[index]); } return 0;}
運行程序:
[purple@localhost popen_write]$ ./main ./reversehow are youyou are howhello worldworld hello
按 ctrl+D 退出程序。
小結1. 在主調程序中,若popen以讀模式打開,說明主調函數需要從管道獲取程序運行結果,因而重定向了被調函數的標準輸出。此時,popen時,主調函數需要將運行被調函數的完整參數寫入放進命令行。被調函數運行后會將運行結果送入管道,主調程序自行從管道取出運行結果,打印在屏幕上。
2. 在主調程序中,若popen以寫模式打開,說明主調函數需要將被調函數的運行所需的參數送人管道,因而重定向了被調函數的標注輸入。此時,popen時,主調函數只需將被調函數的path寫入命令行即可。被調函數會從管道中取走自己的參數,運行結果由被調函數打印在屏幕上。
新聞熱點
疑難解答