标准管道(popen)

NAME

       popen, pclose - pipe stream to or from a process



SYNOPSIS

       #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:[email protected] 

    > 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:[email protected] 

    > 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 ./reverse

how are you

you are how

baby u r beautiful

beautiful r u baby

[purple@localhost popen]$

按 ctrl+D 退出popen.c中的循环,从而退出程序。

popen.c,如下:

/*************************************************************************

    > File Name: popen.c

    > Author: KrisChou

    > Mail:[email protected] 

    > 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:[email protected] 

    > 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 ./reverse

how are you

you are how

hello world

world hello

按 ctrl+D 退出程序。

小结

1. 在主调程序中,若popen以读模式打开,说明主调函数需要从管道获取程序运行结果,因而重定向了被调函数的标准输出。此时,popen时,主调函数需要将运行被调函数的完整参数写入放进命令行。被调函数运行后会将运行结果送入管道,主调程序自行从管道取出运行结果,打印在屏幕上。

2. 在主调程序中,若popen以写模式打开,说明主调函数需要将被调函数的运行所需的参数送人管道,因而重定向了被调函数的标注输入。此时,popen时,主调函数只需将被调函数的path写入命令行即可。被调函数会从管道中取走自己的参数,运行结果由被调函数打印在屏幕上。

你可能感兴趣的:(open)