Linux C-popen () function

Source: Internet
Author: User
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;

}

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.