Linux C-popen () function
# Include <stdio. h>
File * popen (const char * command, const char * type );
Int pclose (File * stream );
Description
The popen () function uses the pipeline creation method and shell. because the pipeline is defined as a single direction, the type parameter can only be defined as read or write. It cannot be the same time for read/write, result stream can only be read or write.
The command counter is a string pointer pointing to a string ending with null. This string contains a shell command. this command is sent to/bin/sh and sent to the-C numeric line, that is, the shell sends the line. the Type struct is also a string that points to the end of a null string.
Pointer, this string must be 'R' or 'W' to determine whether it is linear or progressive.
The Return Value of the popen () function is a common standard I/O stream. It can only be used for the pclose () function, rather than fclose (). function.
For example, the inbound traffic of this stream is converted to the target traffic of the command. The standard output of the command is to use popen () with the cursor (), the process of the function is the same, unless the command changes itself. on the contrary, when retrieving a "popen" stream, it is equivalent to when the command is named again, the Command's standard arguments are the same as the arguments using popen.
Note that the outgoing stream of the popen function is fully occupied.
The pclose function waits for the related progress and returns the exit response of a command, just like the wait4 function.
Example:
# Include <stdio. h>
Int main (INT argc, char * argv [])
{
Char Buf [128];
File * PP;
If (Pp = popen ("ls-L", "R") = NULL)
{
Printf ("popen () error! \ N ");
Exit (1 );
}
While (fgets (BUF, sizeof Buf, pp ))
{
Printf ("% s", Buf );
}
Pclose (PP );
Return 0;
}