Java 如何从c++;喝

Java 如何从c++;喝,java,python,c++,stdout,swig,Java,Python,C++,Stdout,Swig,我有一个C++SWIG库。 我想重定向python中的输出“print”和java中的“System.out.println”。所以我想我需要重定向STDOUT的输出 以下代码在C中工作++ fclose (stdout); std::ofstream out("example.txt"); std::cout.rdbuf(out.rdbuf()); std::cout << "printing example 1" << std

我有一个C++SWIG库。 我想重定向python中的输出“print”和java中的“System.out.println”。所以我想我需要重定向STDOUT的输出

以下代码在C中工作++

fclose (stdout);
std::ofstream out("example.txt");
std::cout.rdbuf(out.rdbuf());
std::cout <<  "printing example 1" << std::endl;
printf("printing example 2");

Out:进程结束,退出代码为-1073741819(0xC0000005)

哪个程序崩溃?这里讨论的有三个。注意:您在流程中没有重定向stdout。您可以
exec
并通过管道将其输入。查看
exec
函数系列,选择一个适合您需要的函数。感谢您的快速响应。我要看不同的“高管家族”。我更新了用python执行的程序。对不起,我很难理解。你能扩展吗?这能回答你的问题吗?
#include <iostream>
#include <fstream>

class StreamRunner{
private:
    std::ofstream file;
public:
    StreamRunner() = default;

    void start(){
        fclose (stdout);
        file.open("out.txt");
        std::streambuf *coutbuf = std::cout.rdbuf();
        std::cout.rdbuf(file.rdbuf());
    }

    void stop(){
        fclose(stdout);
    }
};
from example import *
runner = StreamRunner()
runner.start()
print("hello")
runner.stop()