c++通过管道pipe获取cmd输出的字符

#include <stdio.h>
#include<iostream>
#include<string>
using namespace std;

// 描述:execmd函数执行命令,并将结果存储到result字符串数组中
// 参数:cmd表示要执行的命令
// result是执行的结果存储的字符串数组
// 函数执行成功返回1,失败返回0
int execmd(char* cmd,char* result) {
char buffer[128]; //定义缓冲区
FILE* pipe = _popen(cmd, "r"); //打开管道,并执行命令
if (!pipe) return 0; //返回0表示运行失败

while(!feof(pipe)) {
if(fgets(buffer, 128, pipe)){ //将管道输出到result中
strcat(result,buffer);
}
}
_pclose(pipe); //关闭管道
return 1; //返回1表示运行成功
}

int main(){
char result[1024*4]=""; //定义存放结果的字符串数组
if(1==execmd("rasdial",result)){
printf(result);
cout<<result;
}
system("pause"); //暂停以查看结果
}

原文链接: https://www.cnblogs.com/deemonliang/p/4896353.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    c++通过管道pipe获取cmd输出的字符

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/223376

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月13日 下午12:03
下一篇 2023年2月13日 下午12:03

相关推荐