简体   繁体   English

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

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

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

Try _popen ( <stdio.h> ), the Microsoft's version of the POSIX popen function: 尝试_popen<stdio.h> ),这是POSIX popen函数的Microsoft版本:

#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM