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 );
Routine Required Header Compatibility
_popen <stdio.h> Win 95, Win NT
_wpopen <stdio.h> or <wchar.h> Win NT
For additional compatibility information, see Compatibility in the Introduction.
LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version
Each of these functions 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.
Command to be executed
Mode of returned stream
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:
The calling process can read the spawned commandís standard output via the returned stream.
The calling process can write to the spawned commandís standard input via the returned stream.
Open in binary mode.
Open in text mode.
Note The _popen function returns an invalid file handle, if used in a Windows program, 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, read the section "Creating a Child Process with Redirected Input and Output" in the Win32 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
/* POPEN.C: This program uses _popen and _pclose to receive a
* stream of text from a system process.
void main( void )
/* 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( (chkdsk = _popen( "dir *.c /on /p", "rt" )) == NULL )
exit( 1 );
/* Read pipe until end of file. End of file indicates that
* CHKDSK closed its standard out (probably meaning it
while( !feof( chkdsk ) )
if( fgets( psBuffer, 128, chkdsk ) != NULL )
printf( psBuffer );
/* Close pipe and print return value of CHKDSK. */
printf( "\nProcess returned %d\n", _pclose( chkdsk ) );
Volume in drive C is CDRIVE
Volume Serial Number is 0E17-1702
Directory of C:\dolphin\crt\code\pcode
05/02/94 01:05a 805 perror.c
05/02/94 01:05a 2,149 pipe.c
05/02/94 01:05a 882 popen.c
05/02/94 01:05a 206 pow.c
05/02/94 01:05a 1,514 printf.c
05/02/94 01:05a 454 putc.c
05/02/94 01:05a 162 puts.c
05/02/94 01:05a 654 putw.c
8 File(s) 6,826 bytes
86,597,632 bytes free
Process returned 0