Standard Pipe (popen), standard pipe popen
NAME popen, pclose - pipe stream to or from a processSYNOPSIS #include <stdio.h> FILE *popen(const char *command, const char *type); int pclose(FILE *stream);DESCRIPTION The popen() function opens a process by creating a pipe, forking, and invoking the shell. Since a pipe is by definition unidirectional, the type argument may specify only reading or writing, not both; the resulting stream is correspondingly read-only or write-only. The command argument is a pointer to a null-terminated string contain- ing a shell command line. This command is passed to /bin/sh using the -c flag; interpretation, if any, is performed by the shell. RETURN VALUE The popen() function returns NULL if the fork(2) or pipe(2) calls fail, or if it cannot allocate memory. The pclose() function returns -1 if wait4(2) returns an error, or some other error is detected.
Write
Popen. c, as follows:
/************************************************************************* > File Name: popen.c > Author: KrisChou > Mail:zhoujx0219@163.com > Created Time: Fri 22 Aug 2014 11:07:26 AM CST ************************************************************************/#include <stdio.h>#include <stdlib.h>#include <string.h>int main(int argc, char *argv[]){ char buf[1024]; FILE *fp; while(memset(buf,0,1024),fgets(buf,1024,stdin) != NULL) { fp = popen(argv[1],"w"); fputs(buf,fp); pclose(fp); } return 0;}
The called function reverse. c is as follows:
/*************************************** * *********************************> File Name: reverse. c> Author: KrisChou> Mail: zhoujx0219@163.com> Created Time: sat 23 Aug 2014 11:21:27 am cst ********************************* ***************************************/ # include <stdio. h> # include <stdlib. h> # include <string. h> int main (int argc, char * argv []) {char word [256]; // read the string char buf [256] [256] from the standard input , Index = 0; // Save the string while (memset (word, 0,256), scanf ("% s", word) read from the standard input )! = EOF) {strcpy (buf [index ++], word) ;}index --; while (index> = 0) {printf ("% s ", buf [index]); index --;} printf ("\ n"); return 0 ;}
Run the program:
[purple@localhost popen]$ gcc popen.c -o main[purple@localhost popen]$ gcc reverse.c -o reverse[purple@localhost popen]$ ./main ./reversehow are youyou are howbaby u r beautifulbeautiful r u baby[purple@localhost popen]$
Press ctrl + D to exit the loop in popen. c and exit the program.
LinuxC for popen, pclose
This function should be used to read the content in the current directory.
Popen can start a new process, and ls-l is the name and parameter of the program to be started. Of course, even if the directory is listed
"R" indicates that the calling process can read the output of the newly started process through the returned file pointer. It can also be "w", so that the calling process can read from the standard input by writing content to the file and the newly started process. In fact, this is the pipeline communication between processes.
Popen returns a file pointer. If the returned NULL pointer is 0, an error occurs.
The following while LOOP reads the content of the returned file to the file pointed to by the passed sck file descriptor. fileno converts the file pointer to the file descriptor.
The pclose function closes the pipeline. The returned value is the exit code of the newly started program,
In general, if the program returns 0, it indicates that it is correct. If the value is non-zero, it indicates that the program may be faulty. If the pclose function is incorrect, it will return-1.
What is the national standard for pipeline logo?
All information
Advanced member information
Latest Information
At the same time, 188 people are looking for "pipeline mark national standards" to publish such information so that customers can take the initiative to find you!
Special Power Plant Boiler pipeline for sale
New and Old: unlimited total supply: multi-ton model type: unlimited transaction price: negotiable or negotiable region: Inner Mongolia storage mode: Unlimited
For more information about supply and demand, see Chifeng yuheng Trading Co., Ltd.
Contact: Mr. Xu | location: Inner Mongolia