_popen、_wpopen_popen, _wpopen

创建一个管道并执行命令。Creates a pipe and executes a command.

重要

此 API 不能用于在 Windows 运行时中执行的应用程序。This API cannot be used in applications that execute in the Windows Runtime. 有关详细信息,请参阅通用 Windows 平台应用中不支持的 CRT 函数For more information, see CRT functions not supported in Universal Windows Platform apps.

语法Syntax

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

参数Parameters

commandcommand
要执行的命令。Command to be executed.

模式mode
返回流的模式。Mode of the 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. 函数针对错误返回 NULLThe functions return NULL on an error. 如果错误是无效的参数,例如,如果命令模式是 null 指针,或模式不是有效模式errno设置为EINVALIf 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.

有关这些及其他错误代码的信息,请参阅 _doserrno、errno、_sys_errlist 和 _sys_nerrFor information about these and other error codes, see _doserrno, errno, _sys_errlist, and _sys_nerr.

备注Remarks

_Popen函数创建一个管道,并以异步方式执行使用指定的字符串的命令处理器的衍生的副本命令The _popen function creates a pipe and asynchronously executes a spawned copy of the command processor with the specified string command. 字符串 mode 指定请求的访问类型,如下所示。The character string mode specifies the type of access requested, as follows.

访问模式Access mode 描述Description
“r”"r" 调用进程可使用返回的流读取生成的命令的标准输出。The calling process can read the spawned command's standard output using the returned stream.
“w”"w" 调用进程可使用返回的流写入生成的命令的标准输入。The calling process can write to the spawned command's standard input using the returned stream.
“b”"b" 在二进制模式下打开。Open in binary mode.
“t”"t" 在文本模式下打开。Open in text mode.

备注

如果在 Windows 程序中使用 _popen函数返回无效的文件指针,从而导致程序无限期停止响应。If used in a Windows program, the _popen function returns an invalid file pointer that causes the program to stop responding indefinitely. _popen控制台应用程序中可正常工作。_popen works properly in a console application. 若要创建 Windows 应用程序将重定向输入和输出,请参阅重定向输入和输出创建的子进程Windows SDK 中。To create a Windows application that redirects input and output, see Creating a Child Process with Redirected Input and Output in the Windows SDK.

_wpopen是宽字符版本 _popen;路径参数 _wpopen是宽字符字符串。_wpopen is a wide-character version of _popen; the path argument to _wpopen is a wide-character string. _wpopen_popen行为相同。_wpopen and _popen behave identically otherwise.

一般文本例程映射Generic-Text Routine Mappings

Tchar.h 例程Tchar.h routine 未定义 _UNICODE 和 _MBCS_UNICODE and _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_tpopen_tpopen _popen_popen _popen_popen _wpopen_wpopen

要求Requirements

例程所返回的值Routine 必需的标头Required header
_popen_popen <stdio.h><stdio.h>
_wpopen_wpopen <stdio.h> 或 <wchar.h><stdio.h> or <wchar.h>

有关更多兼容性信息,请参阅 兼容性For more compatibility information, see Compatibility.

Libraries

C 运行时库的所有版本。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, 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");
   }
}

示例输出Sample Output

此输出假定当前目录中只有一个文件扩展名为 .c 的文件。This output assumes that there is only one file in the current directory with a .c file name 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
_pclose_pclose
_pipe_pipe