日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > windows >内容正文

windows

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

發(fā)布時(shí)間:2024/1/23 windows 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 windows下利用_popen,_wopen创建管道进行系统命令输出数据 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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

_popen,_wpopen這是C運(yùn)行庫(當(dāng)然popen函數(shù)為L(zhǎng)inux C)

CreatePipe function這是API函數(shù)

system函數(shù)可以運(yùn)行命令行,并不能獲得顯示結(jié)果,執(zhí)行結(jié)果則是通過管道來完成的。首先用popen打開一個(gè)命令行的管道,然后通過fgets獲得該管道傳輸?shù)膬?nèi)容,也就是命令行運(yùn)行的結(jié)果

一、函數(shù)介紹

? 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

?

轉(zhuǎn)載鏈接:?https://blog.csdn.net/greless/article/details/72383762

總結(jié)

以上是生活随笔為你收集整理的windows下利用_popen,_wopen创建管道进行系统命令输出数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。