国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 操作系統 > 正文

標準管道(popen)

2024-06-28 13:24:36
字體:
來源:轉載
供稿:網友
標準管道(popen)
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寫入命令行即可。被調函數會從管道中取走自己的參數,運行結果由被調函數打印在屏幕上。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 独山县| 曲阜市| 禄丰县| 麦盖提县| 新疆| 班戈县| 咸阳市| 东台市| 太白县| 龙门县| 古蔺县| 衡阳县| 年辖:市辖区| 临桂县| 上虞市| 溧水县| 外汇| 阳朔县| 昌邑市| 商南县| 金阳县| 仙桃市| 抚顺市| 同仁县| 罗源县| 琼中| 泰宁县| 上高县| 老河口市| 自贡市| 邳州市| 台北市| 韶山市| 会昌县| 长乐市| 九台市| 麻城市| 白水县| 寿阳县| 桐柏县| 四川省|