freopen stdout and console
c, stdout, windows
Solution
You should be able to use `_dup` to do this
Something like this should work (or you may prefer the example listed in the `_dup` documentation):
#include <io.h>
#include <stdio.h>
...
{
int stdout_dupfd;
FILE *temp_out;
/* duplicate stdout */
stdout_dupfd = _dup(1);
temp_out = fopen("file.txt", "w");
/* replace stdout with our output fd */
_dup2(_fileno(temp_out), 1);
/* output something... */
printf("Woot!\n");
/* flush output so it goes to our file */
fflush(stdout);
fclose(temp_out);
/* Now restore stdout */
_dup2(stdout_dupfd, 1);
_close(stdout_dupfd);
}
Problem
Given the following function: ``` freopen("file.txt","w",stdout); ``` Redirects stdout into a file, how do I make it so stdout redirects back into the console? I will note, yes there are other questions similar to this, but they are about linux/posix. I'm using windows. You can't assigned to stdout, which nullifies one set of solutions that rely on it. dup and dup2() are not native to windows, nullifying the other set. As said, posix functions don't apply (unless you count fdopen()).