原文来自: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 OutputThis 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

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

  1. Windows下利用python+selenium+firefox爬取动态网页数据(爬取东方财富网指数行情数据)

    由于之前用urlib和request发现只能获取静态网页数据,目前爬取动态网页有两种方法, (1)分析页面请求 (2)Selenium模拟浏览器行为(霸王硬上弓),本文讲的就是此方法 一.安装sele ...

  2. Windows下在Django中创建项目时ImportError: No module named django.core解决方法

    Windows下在Django中创建项目时ImportError: No module named django.core解决方法 今天在对照<Python编程:从入门到实践>学## 标题 ...

  3. Windows下MongoDB安装及创建用户名和密码

    Windows下MongoDB安装及创建用户名和密码 下载MongoDB的安装文件https://www.mongodb.com/download-center#community,选择合适的版本(注 ...

  4. 如何在Windows下利用Apche查看MySQL数据库?

    本篇文章主要跟大家介绍的是如何在Windows下利用Apche查看MySQL数据库,小杜觉得挺实在的,就整理了一下并分享给大家做个参考,希望大家看完之后有一定的收获.因此,有感兴趣的朋友记得要看完! ...

  5. Windows下利用**SDFormatter**格式化SD卡

    树莓派3-SD卡格式化-Windows下利用SDFormatter格式化SD卡 格式化前准备 下载SDFormatter软件 下载 一张Miscro SD卡 读卡器 电脑 格式化步骤 下载软件并安装 ...

  6. Windows下使用C语言创建定时器并周期和网络调试助手通信

    在Windows C下采用timeSetEvent函数来设置定时器 关于timeSetEvent的函数原型及注释如下所示: MMRESULT timeSetEvent(UINT uDelay, // ...

  7. Windows下利用Ghost32一次性完成U盘版PE的制作

    1.工具(1)HP U 盘格式化工具(利用该工具可以将U盘格式化成为本地硬盘,以解决在ghost下看不到U盘的问题) (2)Ghost32 11.0此Ghost 32能够在Windows下运行,可以不 ...

  8. 《Python简明教程》第10章在Windows下利用Haozip命令行备份文件

    在<Python简明教程>第10章中的备份程序使用的是在Linux下压缩命令.由于本人没安装Linux系统,为了实现例子10.1,便需要一个带命令行的压缩程序.国产好压软件刚好带有命令行, ...

  9. windows下利用sox批量将PCM转为WAV

    1 说明 在做语音处理时,需要批量将pcm的裸数据转为wav格式,我们班的女孩子一开始是一个一个手动转换,大约有8万条吧,再一次证明了女人狠起来是真的狠.求助了我,我弄了一个批处理文件给她. 2 实现 ...

最新文章

  1. 《OpenCV3编程入门》学习笔记5 Core组件进阶(五)离散傅里叶变换(DFT)
  2. string:值类型?引用类型?[转]
  3. .net mvc actionresult 返回字符串_字符串游戏之无效的身份证
  4. excel if in函数_【Excel函数】Small+Index+IF 一对N返回
  5. 程序员面试金典 - 面试题 17.25. 单词矩阵(Trie树+DFS回溯,hard)
  6. RabbitMQ的应用场景以及基本原理简介
  7. MFC_选择目录对话框_选择文件对话框_指定目录遍历文件
  8. 01 前言/基础设施 - DevOps之路
  9. android某个界面横屏,iOS强制某个界面横屏的方法
  10. 【树链剖分】树链剖分讲解
  11. linux系统安装红蜘蛛,在linux Deepin深度系统安装多媒体电子教室Veyon
  12. FDE中的金属边界条件和PML边界条件的选取
  13. 程序员租房福利! 最新 2018年上海公积金提取 租房提取
  14. 谷歌开始卷自己,AI架构Pathways加持,推出200亿生成模型
  15. 【Ping检测】使用Ping命令检查网络连接情况
  16. 使用java对文件内容加密
  17. FPGA协同验证方法-资料整理
  18. DS SIMULIA CST STUDIO SUITE 2021.05 SP5
  19. docker 创建容器时指定容器ip
  20. JCR分区(WOS或Thomson Reuters或汤姆森 路透)和中科院分区(附网址及查询方法)_2018年

热门文章

  1. Effective Java之利用有限制通配符提升API的灵活性(二十八)
  2. 算法训练营12-动态规划
  3. DBSAN密度聚类算法
  4. 做系统ghost步骤图解_Ghost 博客搭建超全指南
  5. Kubernetes二进制部署——Flannel网络
  6. 华为交换机 查看IP和MAC对应关系
  7. Linux中关于 su 和 su - 的区别
  8. 使用gcc编译c语言程序,用GCC编译C ++程序
  9. c语言程序设计中北大学,《中北大学软件学院2013届C语言程序设计实训题目.doc...
  10. 异步通知和同步通知_CCF NOI 2020 网上同步赛报名通知