• 13-(1-4)进程管道关于popen(-r-w)及pipe的程序使用实例


    #include<unistd.h>
    #include<stdlib.h>
    #include<stdio.h>
    #include<string.h>
    #define BUFSIZE 200
    int main()
    {
        FILE *read_fp;
        char buffer[BUFSIZE+1];/*用于存放读取的内容*/
        int chars_read;
        memset(buffer,'',sizeof(buffer));
        read_fp=popen("uname -a","r");//用于显示内核版本 及现在时间
        /*返回的是文件指针,因此-文件指针是要定义的*/
        if(read_fp != NULL)
        {
            chars_read=fread(buffer,sizeof(char),BUFSIZE,read_fp);//返回读取字符的个数,错误则为-1
            /*并读取传回的内容是什么*/
            if(chars_read > 0)
            {
                printf("Output was:-
    %s
    ",buffer);
            }
    
            pclose(read_fp);
            exit(EXIT_SUCCESS);
        }
        exit(EXIT_FAILURE);
    }
    /*因些,使用popen到少要定义:
    文件指针,
    字符串
    */
    #include<unistd.h>
    #include<stdlib.h>
    #include<stdio.h>
    #include<string.h>
    #define BUFSIZE 50
    int main()
    {
        FILE *read_fp;
        char buffer[BUFSIZE+1];/*用于存放读取的内容*/
        int chars_read;
        memset(buffer,'',sizeof(buffer));
        read_fp=popen("ps ax","r");//用于显示内核版本 及现在时间
        /*返回的是文件指针,因此-文件指针是要定义的*/
        if(read_fp != NULL)
        {
            while(chars_read > 0)
            {
    
                //返回读取字符的个数,错误则为-1
                /*并读取传回的内容是什么*/
                chars_read=fread(buffer,sizeof(char),BUFSIZE,read_fp);
                buffer[chars_read-1]='';
                printf("Reading %d:-
     %s 
    ",BUFSIZE,buffer);
    
            }
            pclose(read_fp);
            exit(EXIT_SUCCESS);
        }
        exit(EXIT_FAILURE);
        return 0;
    }
    /*因些,使用popen到少要定义:
    文件指针,
    字符串
    */
    #include<unistd.h>
    #include<stdlib.h>
    #include<stdio.h>
    #include<string.h>
    #define BUFSIZE 50
    int main()
    {
        FILE *read_fp;
        char buffer[BUFSIZE+1];/*用于存放读取的内容*/
        int chars_read;
        memset(buffer,'',sizeof(buffer));
        read_fp=popen("cat 13-3-*.cpp | wc -l","r");//用于显示内核版本 及现在时间
        /*返回的是文件指针,因此-文件指针是要定义的*/
        if(read_fp != NULL)
        {
            while(chars_read > 0)
            {
    
                //返回读取字符的个数,错误则为-1
                /*并读取传回的内容是什么*/
                chars_read=fread(buffer,sizeof(char),BUFSIZE,read_fp);
                buffer[chars_read-1]='';
                printf("Reading %d:-
     %s 
    ",BUFSIZE,buffer);
    
            }
            pclose(read_fp);
            exit(EXIT_SUCCESS);
        }
        exit(EXIT_FAILURE);
        return 0;
    }
    /*因些,使用popen到少要定义:
    文件指针,
    字符串
    */
    #include<unistd.h>
    #include<stdlib.h>
    #include<stdio.h>
    #include<string.h>
    
    #define Buffer 100
    
    int main()
    {
        FILE *write_fp;
        char buffer[Buffer + 1];
        //把格式化的数据写入某个字符串缓冲区。
        //返回值:字符串长度(strlen)
        sprintf(buffer,"Once upon a time, there was...
    ");
        //********格式化输出文件中的数据***********///
        //-c:等价于-t c,选择ASCII码字符或者是转义字符
        write_fp=popen("od -c","w");
        if(write_fp != NULL)
        {
            fwrite(buffer,sizeof(char),strlen(buffer),write_fp);
            pclose(write_fp);
            exit(EXIT_SUCCESS);
        }
        exit(EXIT_FAILURE);
    }
    #include <unistd.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        int data_processed;
        int file_pies[2];
        const char some_data[]="123";
        char buffer[BUFSIZ+1];
    
        memset(buffer,'',sizeof(buffer));
    
        if(pipe(file_pies)==0)
        {
            data_processed=write(file_pies[1],some_data,strlen(some_data));
            printf("Wrote %d bytes
    ",data_processed);
            data_processed=read(file_pies[0],buffer,BUFSIZ);
            printf("Read %d bytes: %s
    ",data_processed,buffer);
            exit(EXIT_SUCCESS);
        }
        exit(EXIT_FAILURE);
    }
  • 相关阅读:
    python中常用的模块二
    python中常用的模块一
    python类与类的关系
    python类的成员
    关于python的面向对象
    python内置函数2
    python内置函数
    python fileinput模块
    python生成器
    python 迭代器
  • 原文地址:https://www.cnblogs.com/orangebook/p/3407602.html
Copyright © 2020-2023  润新知