_pclose

From RAD Studio
Jump to: navigation, search

Go Up to stdio.h Index


Header File

stdio.h

Category

Input/output Routines, Process Control Routines

Prototype

int _pclose(FILE * stream);

Description

Waits for piped command to complete.

_pclose closes a pipe stream created by a previous call to _popen, and then waits for the associated child command to complete.

Return Value

On success, _pclose returns the termination status of the child command. This is the same value as the termination status returned by cwait, except that the high and low order bytes of the low word are swapped.

On error, it returns -1.

Example

/* this program initiates a child process to run the dir command
  and pipes the directory listing from the child to the parent.
*/

#include <stdio.h>     // popen() pclose() feof() fgets() puts()
#include <string.h>    // strlen()

int main( )
{
 FILE* handle;        // handle to one end of pipe
 char message[256];   // buffer for text passed through pipe
 int status;          // function return value

 // open a pipe to receive text from a process running "DIR"

 handle = _popen("dir /b", "rt");
 if (handle == NULL)
 {
   perror("_popen error");
 }

 // read and display input received from the child process
 while (fgets(message, sizeof(message), handle))
 {
   fprintf(stdout, message);
 }

 // close the pipe and check the return status
   status = _pclose(handle);
 if (status == -1)
 {
   perror("_pclose error");
 }

 return(0);
}

Portability

POSIX Win32 ANSI C ANSI C++

+