zoukankan      html  css  js  c++  java
  • 标准管道的使用

    FILE * popen ( const char * command , const char * type );

    第一个参数是命令,不是文件名

    popen() 函数通过创建一个管道,调用 fork 产生一个子进程,执行一个 shell 以运行命令来开启一个进程。这个进程必须由 pclose() 函数关闭,而不是 fclose() 函数。pclose() 函数关闭标准 I/O 流,等待命令执行结束,然后返回 shell 的终止状态。如果 shell 不能被执行,则 pclose() 返回的终止状态与 shell 已执行 exit 一样。

    标准管道是重定向标准输入和输出的

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

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

    //写模式popen.c

    #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因为主调函数写,这里的被重定向读,标准读scanf从主调函数里面获取数据
    #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("
    ");
        return 0;
    }

    gcc popen.c -o main

    gcc reverse.c -o reverse
     ./main ./reverse
    how are you
    you are how

    //读模式
    popen.c
    #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因为主调函数读,这里的被重定向写,标准写printf将数据发送到主调函数要读的管道中
    #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;
    }

    ./main ./reverse
    how are you
    you are how
    hello world
    world hello


  • 相关阅读:
    Invalid column name on sql server update after column create
    个人工资计算表
    xxxx
    excel cannot access the file there are several possible reasons
    .NET/VB.NET: solving the error “The system cannot find the file specified.” “Temp.NETFramework,Version=v4.0.AssemblyAttributes.vb”
    GIT
    时区
    create Excel file
    开发类分组
    判断是否已安装.net framework
  • 原文地址:https://www.cnblogs.com/hxjbc/p/3959737.html
Copyright © 2011-2022 走看看