I have a C ++ SWIG library. I would like to redirect the output "print" in python and "System.out.println" in java. So I think I need to redirect the output of STDOUT.
the following code works in C ++
fclose (stdout);
std::ofstream out("example.txt");
std::cout.rdbuf(out.rdbuf());
std::cout <<  "printing example 1" << std::endl;
printf("printing example 2");
But in python the line "fclose (stdout);" crashes the program:
Process finished with exit code -1073741819 (0xC0000005)
What am I doing wrong?
I am on windows, I am using SWIG 4.0 and c ++ 17
EDIT : Following Tadman's commentary
Here is the test program I am running :
My C ++ class :
#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);
    }
};
My python file :
from example import *
runner = StreamRunner()
runner.start()
print("hello")
runner.stop()
Out : Process finished with exit code -1073741819 (0xC0000005)