I. Introduction to Functions
1.1 function prototype:
Copy codeThe Code is as follows:
# Include <stdio. h>
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
Copy codeThe Code is as follows:
// OutputTest. c
# Include <stdio. h>
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
Copy codeThe Code is as follows:
// InputTest. c
# Include <stdio. h>
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:
Copy codeThe 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 % 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:
Copy codeThe 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 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:
Copy codeThe 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:
Copy codeThe Code is as follows:
#! /Usr/bin/python
Import OS
OS. popen ('./inputtest', 'w'). write ("test ")
The running effect is as follows: