The popen function in the python foundation tutorial manipulates the input and output examples of other programs

  • 2020-04-02 13:24:00
  • OfStack

I. function introduction

1.1 function prototype:


#include <stdio.h>
FILE *popen(const char *command,const char *open_mode);

1.2 illustrates

The popen function allows a program to start another program as a new process and pass data to it or receive data from it. The command string is the name of the program to run and the corresponding parameters (such as ls or ls-l), and openmode must be r or w. If r, the output of the called program can be used by the calling program. If it is w, the calling program can use fwrite to send data to the called program as its input on the standard input stream.

Ii. Preparation of test procedures

Here are two very simple programs for the following test.

2.1 output the test program


// outputTest.c
#include <stdio.h>
int main()
{
        printf("Just a test ! n");
        return 0;
}

The main implementation is to output a string to the standard output device, for the following program to test.

2.2 input the test program


// inputTest.c
#include <stdio.h>
int main()
{
        char buf[1024] = {0};
        scanf("%s",buf);
        printf("your input : %sn",buf);
        return 0;
}

The main implementation from the standard input device input string and output, for the following program to test.

3. Popen operation example (C code)

3.1 obtain program output

To test with the outputTest program, the code is as follows:


#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main()
{
        FILE *read_fp;
        char buffer[BUFSIZ + 1];
        int chars_read;
        memset(buffer,'0',sizeof(buffer));
        read_fp = popen("./outputTest","r");
        if(read_fp != NULL)
        {
                chars_read = fread(buffer,sizeof(char),BUFSIZ,read_fp);
                if(chars_read > 0)
                {
                        printf("Output was : n%snDonen",buffer);
                }
                pclose(read_fp);
                exit(EXIT_SUCCESS);
        }
        exit(EXIT_FAILURE);
}

The operation effect is as follows:

< img SRC = "border = 0 / / img.jbzj.com/file_images/article/201402/20140210114319.jpg? 2014110115318 ">

Here, the output of the called program is mainly obtained with the r parameter.

3.2 pass parameters to other programs

To test with inputTest, the code is as follows:


#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main()
{
        FILE *write_fp;
        char buffer[BUFSIZ + 1];
        sprintf(buffer,"Once...n");
        write_fp = popen("./inputTest","w");
        if(write_fp != NULL)
        {
                fwrite(buffer,sizeof(char),strlen(buffer),write_fp);
                pclose(write_fp);
                exit(EXIT_SUCCESS);
        }
        exit(EXIT_FAILURE);
}

The operation effect is as follows:

< img SRC = "border = 0 / / img.jbzj.com/file_images/article/201402/20140210114346.jpg? 2014110115252 ">

Here, we mainly use the w parameter to pass parameters to the called program.

4. Poepn operation example (python code)

You can play with python that way.

4.1 obtain program output

Take the outputTest program mentioned above as an example, and the code is as follows:


#! /usr/bin/python
import os
#var = os.popen('ls -l').read()
var = os.popen('./outputTest').read()
print var

The operation effect is as follows:
< img border = 0 id = theimg onclick = window. The open this. (SRC) SRC = "/ / img.jbzj.com/file_images/article/201402/0140210114402.jpg? 2014110114647 ">

4.2 pass parameters to other programs

Take the inputTest program mentioned above as an example, and the code is as follows:


#! /usr/bin/python
import os
os.popen('./inputTest','w').write("test")

The operation effect is as follows:

< img border = 0 id = theimg onclick = window. The open this. (SRC) SRC = "/ / img.jbzj.com/file_images/article/201402/20140210114412.jpg? 2014110114453 ">  


Related articles: