想利用管道技术,查看了一下msdn,发现_popen() 好像挺好使的,将它的例程搬到我的程序里面,发现不能够正常运行,错误出在创建并打开管道上_popen() 。后来重新建了一个console 程序,再将例程放进去,发现运行正常。估计是这个函数不能够在子线程中运行,期望高手指点。附例程:#include <stdio.h>
#include <stdlib.h>void main( void )
{ char psBuffer[128];
FILE *chkdsk; /* 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
* terminated).
*/
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 ) );
}
#include <stdlib.h>void main( void )
{ char psBuffer[128];
FILE *chkdsk; /* 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
* terminated).
*/
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 ) );
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货