首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

问下高手 C语言 怎么获取dos命令行返回的结果

2012-04-09 
问下高手 C语言 如何获取dos命令行返回的结果C语言如何获取dos命令行返回的结果比如我用用system( ipconf

问下高手 C语言 如何获取dos命令行返回的结果
C语言   如何获取dos命令行返回的结果
比如我用   用system( "ipconfig   -all ");
我想在C语言中获得dos下执行这个命令的结果   怎么做啊,
另外如何使调用dos命令不使cmd窗口不闪

[解决办法]
system( "ipconfig -all> > c:\123.txt ");
[解决办法]
system( "ipconfig -all> > a.txt ");
再调用读文件的方法取得所需的值
再加上system( "pause ");
使cmd窗口不闪

[解决办法]
_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 );

Example

/* POPEN.C: This program uses _popen and _pclose to receive a
* stream of text from a system process.
*/

#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 ) );
}


Output

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


MSDN 上的例子

热点排行