_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 );
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. If the error is an invalid parameter, such as if command or mode is a null pointer, or mode is not a valid mode, errno is set to EINVAL. See the Remarks section for valid modes.
For information about these and other error codes, see _doserrno, errno, _sys_errlist, and _sys_nerr.
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.
_wpopen is a wide-character version of _popen; the path argument to _wpopen is a wide-character string. _wpopen and _popen behave identically otherwise.
Tchar.h routine | _UNICODE and _MBCS not defined | _MBCS defined | _UNICODE defined |
|---|---|---|---|
_tpopen | _popen | _popen | _wpopen |
Routine | Required header |
|---|---|
_popen | <stdio.h> |
_wpopen | <stdio.h> or <wchar.h> |
For more compatibility information, see Compatibility in the Introduction.
Libraries
All versions of the C run-time libraries.
// 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, or an error occurs. */
while(fgets(psBuffer, 128, pPipe))
{
printf(psBuffer);
}
/* Close pipe and print return value of pPipe. */
if (feof( pPipe))
{
printf( "\nProcess returned %d\n", _pclose( pPipe ) );
}
else
{
printf( "Error: Failed to read the pipe to the end.\n");
}
}
Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.
Note: