redirect stdout/stderr to file under unix c++ - again

前端 未结 6 1012
说谎
说谎 2021-02-15 15:51

What I want to do

redirect stdout and stderr to one or more files from inside c++

Why I need it

I am using an external, pr

6条回答
  •  情话喂你
    2021-02-15 16:05

    For C++ iostreams, you can use the non-const overload of rdbuf to set std::cout to a std::filebuf. (This is best done by means of an RAII class, since you have to restore it before leaving main.) For C FILE*, you can use freopen, but I don't think you'll be able to restore it.

    FWIW: both of these solutions use only standard C++ or C, so should be portable.

提交回复
热议问题