将文本信息从 Windows 命令解释器 (cmd.exe) 管道到字符数组

Piping text information from the Windows command interpreter (cmd.exe) to a character array

本文关键字:exe 管道 数组 字符 cmd 信息 文本 Windows 解释器 命令      更新时间:2023-10-16

有没有办法在不从命令解释器创建文本文件的情况下将文本信息从Windows命令解释器(cmd.exe)提取到字符数组中?

尝试_popen<stdio.h> ),Microsoft版本的 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;
}