windows 的_popen,与linux 的popen对应

Run-Time Library Reference 

_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 the returned stream.

 

测试程序:(vc6下编译通过)

#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", "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");

   }

}

发布了55 篇原创文章 · 获赞 69 · 访问量 50万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览