c++: subprocess output to stdin

不羁的心 提交于 2019-11-29 09:39:24
dasblinkenlight

Using popen skips the file, and gets you command's output through an in-memory buffer.

#include <iomanip>
#include <iostream>
using namespace std;
const int MAX_BUFFER = 255;
int main() {
    string stdout;
    char buffer[MAX_BUFFER];
    FILE *stream = popen("command", "r");
    while ( fgets(buffer, MAX_BUFFER, stream) != NULL )
        stdout.append(buffer);
    pclose(stream);
    cout << endl << "output: " << endl << stdout << endl;
}

If you happen to be on windows :

follow this : http://support.microsoft.com/kb/190351

It describes it better than I ever would. You can redirect everything, everywhere.

The best thing to do is open the shell command using popen(). This allows you to pass a command and get back a FILE* pointer to the output. See http://www.lix.polytechnique.fr/~liberti/public/computing/prog/c/C/FUNCTIONS/popen.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!