The popen function allows a program to start another program as a new process and transmit data to it or receive data through it. the following example shows how to use the function.
1.1 Function prototype:
The code is as follows:
# Include
FILE * popen (const char * command, const char * open_mode );
1.2 Description
The popen function allows a program to start another program as a new process and transmit data to it or receive data through it. The command string is the program name to run and the corresponding parameter (such as ls or ls-l). openmode must be r or w. If it is r, the output of the called program can be used by the program calling it; if it is w, the caller can use fwrite to send data to the called program as its input in the standard input stream.
2. test program preparation
Prepare two simple programs for the test below.
2.1 output test program
The code is as follows:
// OutputTest. c
# Include
Int main ()
{
Printf ("Just a test! \ N ");
Return 0;
}
The main function is to output a string to the standard output device for the following program to test.
2.2 enter the test program
The code is as follows:
// InputTest. c
# Include
Int main ()
{
Char buf [1024] = {0 };
Scanf ("% s", buf );
Printf ("your input: % s \ n", buf );
Return 0;
}
It is mainly used to input and output strings from the standard input device for testing in the following program.
3. popen operation example (C code)
3.1 obtain program output
Use the outputTest program for testing. the code is as follows:
The code is as follows:
# Include
# Include
# Include
# Include
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 % s \ nDone \ n", buffer );
}
Pclose (read_fp );
Exit (EXIT_SUCCESS );
}
Exit (EXIT_FAILURE );
}
The running effect is as follows:
Here, the r parameter is used to obtain the output of the called program.
3.2 pass parameters to other programs
Use inputTest for testing. the code is as follows:
The code is as follows:
# Include
# Include
# Include
# Include
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 running effect is as follows:
Here, the w parameter is used to transmit parameters to the called program.
4. poepn operation example (python code)
In fact, python can also be used like this.
4.1 obtain program output
The outputTest program mentioned above is used as an example. the code is as follows:
The code is as follows:
#! /Usr/bin/python
Import OS
# Var = OS. popen ('ls-L'). read ()
Var = OS. popen ('./outputtest'). read ()
Print var
The running effect is as follows:
4.2 pass parameters to other programs
The inputTest program mentioned above is used as an example. the code is as follows:
The code is as follows:
#! /Usr/bin/python
Import OS
OS. popen ('./inputtest', 'w'). write ("test ")
The running effect is as follows: