Export (0) Print
Expand All
Expand Minimize

_popen, _wpopen

Creates a pipe and executes a command.

FILE *_popen(
   const char *command,
   const char *mode 
);
FILE *_wpopen(
   const wchar_t *command,
   const wchar_t *mode 
);

Parameters

command
Command to be executed.
mode
Mode of returned stream.

Return Value

Returns a stream associated with one end of the created pipe. The other end of the pipe is associated with the spawned command's standard input or standard output. The functions return NULL on an error.

Remarks

The _popen function creates a pipe and asynchronously executes a spawned copy of the command processor with the specified string command. The character string mode specifies the type of access requested, as follows:

"r"
The calling process can read the spawned command's standard output via the returned stream.
"w"
The calling process can write to the spawned command's standard input via the returned stream.
"b"
Open in binary mode.
"t"
Open in text mode.
Note   If used in a Windows program, the _popen function returns an invalid file pointer that will cause the program to hang indefinitely. _popen works properly in a Console application. To create a Windows application that redirects input and output, see Creating a Child Process with Redirected Input and Output in the Platform SDK.

_wpopen is a wide-character version of _popen; the path argument to _wpopen is a wide-character string. _wpopen and _popen behave identically otherwise.

Generic-Text Routine Mappings

TCHAR.H routine _UNICODE & _MBCS not defined _MBCS defined _UNICODE defined
_tpopen _popen _popen _wpopen

Requirements

Routine Required header Compatibility
_popen <stdio.h> Win 98, Win Me, Win NT, Win 2000, Win XP
_wpopen <stdio.h> or <wchar.h> Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.

Libraries

All versions of the C run-time libraries.

Example

// crt_popen.c
/* This program uses _popen and _pclose to receive a 
 * stream of text from a system process.
 */

#include <stdio.h>
#include <stdlib.h>

int main( void )
{

   char   psBuffer[128];
   FILE   *pPipe;

        /* Run DIR so that it writes its output to a pipe. Open this
         * pipe with read text attribute so that we can read it 
         * like a text file. 
         */

   if( (pPipe = _popen( "dir *.c /on /p", "rt" )) == NULL )
      exit( 1 );

        /* Read pipe until end of file. */

   while( !feof( pPipe ) )
   {
      if( fgets( psBuffer, 128, pPipe ) != NULL )
         printf( psBuffer );
   }

        /* Close pipe and print return value of pPipe. */

   printf( "\nProcess returned %d\n", _pclose( pPipe ) );
}

Sample Output

This output assumes that there is only one file in the current directory with a .c extension.

 Volume in drive C is CDRIVE
 Volume Serial Number is 0E17-1702

 Directory of D:\proj\console\test1

07/17/98  07:26p                   780 popen.c
               1 File(s)            780 bytes
                             86,597,632 bytes free

Process returned 0

See Also

Process and Environment Control Routines | _pclose | _pipe | Run-Time Routines and .NET Framework Equivalents

Show:
© 2014 Microsoft