11

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()).

SE Does Not Like Dissent
  • 1,767
  • 3
  • 16
  • 36
  • 1
    Did you try some of the ones posted [here](http://stackoverflow.com/questions/1908687/how-to-redirect-the-output-back-to-the-screen-after-freopenout-txt-a-stdou)? How did they fail? (BTW, freopen is POSIX and available on Windows. Windows has some POSIX support.) – Mat Oct 05 '11 at 16:53
  • 1
    @Mat: Those were for a posix system, as per the question, windows is in use here. If it has posix support you'll have to clarify what (functions) it supports in relation. – SE Does Not Like Dissent Oct 05 '11 at 16:54
  • [freopen](http://msdn.microsoft.com/en-us/library/wk2h68td(v=vs.80).aspx) - look at the links on the left. I don't have to clarify anything. You post a question with a function call that is valid on Windows, and there are similar questions that also use APIs available on Windows. (The one that needs to do the research is yourself - you're the one stuck with the problem.) – Mat Oct 05 '11 at 16:58
  • 2
    `you'll have to` Not really @Mat,or rather anyone else doesn't need to , No one gets paid to help *you* here. Atleast show a little gratitude instead of *demanding* towards the ones helping you. – Alok Save Oct 05 '11 at 16:58
  • 2
    @Als: I am not familiar with the system. If I knew what did and did not work, I would not be asking a question on it. He'll have to show me because I won't know myself. – SE Does Not Like Dissent Oct 05 '11 at 17:00

5 Answers5

18

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);
}
Hasturkun
  • 35,395
  • 6
  • 71
  • 104
  • Is it possible to write explicitly `stdout` instead of all this ones? – Sandburg Jul 08 '22 at 12:40
  • @Sandburg: If this wasn't Windows specific, You could include `unistd.h` and write `STDOUT_FILENO` instead (or define `STDOUT_FILENO` to 1 yourself). You could also use `_fileno` to get the `stdout` fd before all of this. – Hasturkun Jul 10 '22 at 11:01
17

An alternate solution is:

freopen("CON","w",stdout);

Per wikipedia "CON" is a special keyword which refers to the console.

dtyler
  • 1,187
  • 6
  • 11
  • 1
    I tried this - Win7, VS2017 - unfortunately it did no work – Thomas Schmid Sep 25 '18 at 08:53
  • 2
    I tested it and yes, it does work. It's the solution I was looking for. Thank you very much for your help @dtyler – gyurix Oct 03 '18 at 19:26
  • 1
    In which environment? For my only the solution with _dup2() worked. – Thomas Schmid Oct 04 '18 at 09:35
  • actually CON: AUX: etc are simply the same as COM1: or A: or C: .. in other words. devices. just that unix places those only in /dev/ as physical 'files' and in dos they are virtually present 'in every directory' (most versions actually have a bug where dir shows them if you specifically type in those names) not so much a 'special keyword'. just a filename that refers to a device. like any other character or block device. – HRH Sven Olaf of CyberBunker Dec 09 '21 at 17:55
4

After posting the answer I have noticed that this is a Windows-specific question. The below still might be useful in the context of the question to other people. Windows also provides _fdopen, so mayble simply changing 0 to a proper HANDLE would modify this Linux solution to Windows.

stdout = fdopen(0, "w")

#include <stdio.h>
#include <stdlib.h>
int main()
{
    freopen("file.txt","w",stdout);
    printf("dupa1");
    fclose(stdout);
    stdout = fdopen(0, "w");
    printf("dupa2");
    return 0;
}
RushPL
  • 4,732
  • 1
  • 34
  • 44
0

take note that the filedescriptors for stdin, stdout, stderr (0,1,2) are not nessesarily the same as the 'special variables' printf() and the likes use. although in most cases they output to the same devices upon program start. (not if you start changing things in the middle of your program, or tty redirects are in place). stdin stdout stderr are FILE * pointers. both concepts need to be 'redirected' seperately from each other with their own methods... 'dup2' is for duplicating file descriptors. not FILE pointers. for FILE * pointers such as stdin, stdout, stderr... 'freopen()'.. but that will literally only affect printf and derivatives.

0

This works to me

#include <stdio.h>
int main()
{

    FILE* original_stdout = stdout;
    stdout = fopen("new_stdout.txt", "w");
    printf("ciao\n");
    fclose(stdout);
    stdout = original_stdout;
    printf("a tutti\n");
    return 0;
}
volperossa
  • 1,339
  • 20
  • 33