There is plenty of documentation about redirecting stdout and stderr to a file instead of the console. How do you redirect it back again? The code below shows my intention, but outputs "stdout is printed to console" only once.
I'm guessing I need to grab the console output buffer, store it somewhere, redirect stdout to file, then restore the console buffer?
#pragma warning(disable:4996)
#include <cstdio>
int main()
{
std::printf("stdout is printed to console\n");
if (std::freopen("redir.txt", "w", stdout)) {
std::printf("stdout is redirected to a file\n"); // this is written to redir.txt
std::fclose(stdout);
std::printf("stdout is printed to console\n");
}
getchar();
return 0;
}