windows下利用_popen,_wopen创建管道进行系统命令输出数据

原文来自:https://msdn.microsoft.com/en-us/library/96ayss4b.aspx

_popen,_wpopen这是C运行库(当然popen函数为Linux C)

CreatePipe function这是API函数

system函数可以运行命令行,并不能获得显示结果,执行结果则是通过管道来完成的。首先用popen打开一个命令行的管道,然后通过fgets获得该管道传输的内容,也就是命令行运行的结果

一、函数介绍

  1._popen

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

mode:

"r"
The calling process can read the spawned command's stantard output using the returned stream

"w"
The calling process can write to the spawned command's standard input using the returned stream.

"b"
Open in binary mode.

"t"
Open in text mode.

   2._pclose

int _pclose(
    FILE *stream
);

     Generic-Text Routine Mappings

Tchar.h routine_UNICODE and _MBCS not defined_MBCS defined_UNICODE defined
_tpopen_popen_popen_wpopen

二、案例

  1._popen

#include "stdafx.h"
#include "stdlib.h"
int main() 
{
    FILE *fp;
    char buf[255] = {0};
    if ((fp = _popen("ipconfig", "r")) == NULL) {
        perror("Fail to popen\n");
        exit(1);
    }
    while (fgets(buf, 255, fp) != NULL) {
        printf("%s", buf);
    }

    _pclose(fp);
    return 0;
}

  2._wpopen

#include "stdafx.h"
#include "stdlib.h"
int main()
{
    FILE *fp;
    char buf[255] = {0};
    if ((fp = _wpopen(_T("ipconfig"), _T("r"))) == NULL) {
        perror("Fail to popen\n");
        exit(1);
    }
    while (fgets(buf, 255, fp) != NULL) {
        printf("%s", buf);
    }
    _pclose(fp);
    return 0;
}

 3.sample

//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



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

参考:

http://www.linuxidc.com/Linux/2011-04/34092.htm

 

转载链接: https://blog.csdn.net/greless/article/details/72383762

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值