C++ 将文本信息从Windows命令解释器(cmd.exe)传输到字符数组

C++ 将文本信息从Windows命令解释器(cmd.exe)传输到字符数组,c++,windows,C++,Windows,有没有一种方法可以将Windows命令解释器(cmd.exe)中的文本信息提取到字符数组中,而无需从命令解释器中创建文本文件?试试\u popen(),微软版本的POSIXpopen函数: #include <stdio.h> #include <iostream> #include <string> using namespace std; int main(void) { FILE * pp; char buf[1024]; s

有没有一种方法可以将Windows命令解释器(cmd.exe)中的文本信息提取到字符数组中,而无需从命令解释器中创建文本文件?

试试
\u popen
),微软版本的POSIX
popen
函数:

#include <stdio.h>
#include <iostream>
#include <string>

using namespace std;

int main(void) {
    FILE * pp;
    char buf[1024];
    string result;

    if ((pp = _popen("dir", "r")) == NULL) {
        return 0;
    }

    while (fgets(buf, sizeof(buf), pp)) {
        result += buf;
    }

    _pclose(pp);

    cout << result << endl;

    return 0;
}
#包括
#包括
#包括
使用名称空间std;
内部主(空){
文件*pp;
char-buf[1024];
字符串结果;
if((pp=_popen(“dir”,“r”))==NULL){
返回0;
}
while(fgets(buf,sizeof(buf),pp)){
结果+=buf;
}
_pclose(pp);
coutTry
\u popen
),微软版本的POSIX
popen
功能:

#include <stdio.h>
#include <iostream>
#include <string>

using namespace std;

int main(void) {
    FILE * pp;
    char buf[1024];
    string result;

    if ((pp = _popen("dir", "r")) == NULL) {
        return 0;
    }

    while (fgets(buf, sizeof(buf), pp)) {
        result += buf;
    }

    _pclose(pp);

    cout << result << endl;

    return 0;
}
#包括
#包括
#包括
使用名称空间std;
内部主(空){
文件*pp;
char-buf[1024];
字符串结果;
if((pp=_popen(“dir”,“r”))==NULL){
返回0;
}
while(fgets(buf,sizeof(buf),pp)){
结果+=buf;
}
_pclose(pp);

不能运行类似“dir”的命令然后将结果以字符串形式而不是放在控制台或文件中?这就是
\u popen
:@mikedu95的用法。你应该将其作为答案发布。@JonathanMee不确定我对这个问题的理解,但现在完成了,谢谢:)@mikedu95这看起来像是我的正确答案+1i.e.运行类似“dir”的命令然后以字符串形式而不是控制台或文件形式检索结果?这就是
\u popen
:@mikedu95的用法。你应该将其作为答案发布。@JonathanMee不确定我对问题的理解,但现在完成了,谢谢:)@mikedu95这看起来像是我+1的确切答案