13-(1-4)プロセスパイプpopen(-r-w)およびpipeのプログラム使用例について

13357 ワード

#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,'\0',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,'\0',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]='\0';
            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,'\0',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]='\0';
            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,'\0',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); }