You crash because wofstream fout("/dev/null");
is created on the stack and is destroyed as soon as you quit the function.
Treat "/dev/null" exactly like you treat the file. That is, call OutputErrorsToFile("/dev/null")
inside StopOutput()
. In fact you don't even need /dev/null, just use wcout.rdbuf(NULL)
, should work faster too.
To write into the file immediately call flush or use std::flush manipilator.