Popen reads and writes from the process

Source: Internet
Author: User
Popen () function introduction function definition FILE * popen (constchar * command, constchar * type); intpclose (FILE * stream); function description popen () function creates a pipeline, call fork to generate a sub-process ,...

Introduction to popen () functions
Function definition
FILE * popen (const char * command, const char * type );
Int pclose (FILE * stream );
Function description
The popen () function creates a pipeline, calls fork to generate a sub-process, and executes a shell to run the command to start a process. This process must be disabled by the pclose () function, rather than the fclose () function. The pclose () function closes the standard I/O stream, waits until the command execution ends, and returns the shell termination status. If the shell cannot be executed, the termination status returned by pclose () is the same as that returned by the shell.
The type parameter can only be read or written. the returned value (standard I/O stream) also has a read-only or write-only type corresponding to the type. If the type is "r", the file pointer is connected to the standard output of the command; if the type is "w", the file pointer is connected to the standard input of the command.
The command parameter is a pointer to a shell command string ending with NULL. This line of command will be passed to bin/sh and The-c flag will be used, shell will execute this command.
The return value of popen is a standard I/O stream and must be terminated by pclose. As mentioned above, this stream is unidirectional. Therefore, writing content to this stream is equivalent to writing standard input to this command. The standard output of the command is the same as that of the process that calls popen. In contrast, reading data from a stream is equivalent to reading the standard output of a command. The standard input of a command is the same as that of a process that calls popen.
Return value
If fork () or pipe () fails to be called, or the memory cannot be allocated, NULL is returned. Otherwise, the standard I/O stream is returned.
Error returned
Popen does not set errno for memory allocation failure.
If an error occurs when fork () or pipe () is called, errno is set to the corresponding error type.
If the type parameter is invalid, errno returns EINVAL.
Example:
/* Calc. c-calculator front-end which uses bc for everything */
2
3/* This is a very simple calculator which uses the external bc
4 command to do everything. It opens a pipe to bc, reads a command
5 in, passes it to bc, and exits .*/
6 # include
7 # include
8 # include
9
10 int main (void ){
11 char buf [1024];
12 FILE * bc;
13 int result;
14
15/* open a pipe to bc, and exit if we fail */
16 bc = popen ("bc", "w");/* call the calculator function in linux, and you can see that it is to write data to the calculator process */
17 if (! Bc ){
18 perror ("popen ");
19 return 1;
20}
21
22/* prompt for an expression, and read it in */
23 printf ("expr:"); fflush (stdout);/* Display input data immediately */
24 fgets (buf, sizeof (buf), stdin);/* get data from standard input */
25
26/* send the expression to bc for evaluation */
27 fprintf (bc, "% s \ n", buf );
28 fflush (bc );
29
30/* close the pipe to bc, and wait for it to exit */
31 result = pclose (bc );
32
33 if (! WIFEXITED (result ))
34 printf ("(abnormal exit) \ n ");
35
36 return 0;
}
"Calc. c" [converted] 3
The running effect is as follows:
Root @ ubuntu :~ /Code #./calc
 
Expr: 5*2
 
10
 
Root @ ubuntu :~ /Code #
Author "pstary"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.