To temporarily redirect stdout
to a file, I'm doing:
printf("Before");
freopen_s(&stream, "test.txt", "w", stdout);
printf("During");
freopen_s(&stream, "CONOUT$", "w", stdout);
printf("After");
That works, however doing:
CONSOLE_SCREEN_BUFFER_INFO sbi = {0};
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &sbi);
No longer seems to work. It is returning false and GetLastError()
is returning 6
which translates into the message "The handle is invalid." with FormatMessage
.
Any advice on why the handle might be invalid? Interestingly, printf
continues to work as expected and SetConsoleTextAttribute
even works with the same handle.