I’m writing a unit test and therefore, cannot change the code within the file that I’m testing. The code that I’m testing has messages in cout that I am trying to redirect into a file to check to make sure that the program is outputting the right messages. Does anyone have a way to redirect stdout in another program that won’t cause a lag? I have tried freopen() and that causes my program to hang for some reason.
Share
You could create a
filebufthen replacecout‘s streambuf with it:You need to restore
cout‘s original streambuf again (or set it to a null pointer) or it will probably crash when the global streams are flushed and destroyed, because thefilebufwill already have gone out of scope.