0

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)

MrStan12
  • 55
  • 6
  • Crashes which program? You've got three you're talking about here. – tadman May 30 '21 at 15:33
  • Note: You don't redirect stdout in your process. You `exec` and feed that over a pipe. Look at the `exec` family of functions and pick one that suits your needs. – tadman May 30 '21 at 15:34
  • Thank you sir for your quick response. I'm going to watch the differents "exec family". I updated with the program executed in python. – MrStan12 May 30 '21 at 16:52
  • I'm sorry, I have a hard time understanding. Can you expand ? – MrStan12 May 30 '21 at 19:50
  • Does this answer your question? [How do I execute a command and get the output of the command within C++ using POSIX?](https://stackoverflow.com/questions/478898/how-do-i-execute-a-command-and-get-the-output-of-the-command-within-c-using-po) – tadman May 31 '21 at 03:25
  • Oh okay I think no or I really don't understand how to apply it to my case. I don't want to run a CMD command and grab the output. I want to be able to run a function and get the stdout output. – MrStan12 May 31 '21 at 12:53
  • I have a static C ++ library which I use in Python and Java. This library defines a class which is redefined in the target language. My library takes a pointer from this class and calls a function. For example a HelloHorld () function which printf ("helloworld") (or print ("helloworld") on python) Until then everything works perfectly. the function is called correctly in the target language In my C ++ code I want to be able to retrieve the STDOUT output of this function. – MrStan12 May 31 '21 at 12:54
  • I thought I would have to start a stream before the execution of the function and stop it after its execution to retrieve the value printed in a variable or a file. And that's why I put this example in python above – MrStan12 May 31 '21 at 12:54
  • I'm sorry I'm a student. Maybe my thinking is totally flawed. My library is similar to a classic unit testing framework and that's why I need to get the "Print" instructions. Thank you for the time you give me – MrStan12 May 31 '21 at 12:55
  • maybe I have to recreate a post explaining my problem better? – MrStan12 May 31 '21 at 13:10
  • Closing `stdout` may throw a number of errors. Also, if you close stdout, you will not get any response after calling `start` - no response in the python terminal. You want to consider redirecting output to a file in a different way. – Jens Munk May 31 '21 at 19:45

0 Answers0