Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Freopen() in UNIX

  1. May 15, 2007 #1
    So I'm using freopen to redirect output from stdout to a file... How do I redirect it back to the screen?

    relevant code:

    FILE *stream = freopen("data.txt", "w", stdout);

    /* print stuff */

    How the heck do I get the output stream to point to the console again?
  2. jcsd
  3. May 15, 2007 #2


    User Avatar
    Science Advisor
    Homework Helper

    On Unix, you can access "the screen" (and every other device) via a filename. /dev/tty is a good bet for what stdout was attached to.

    freopen("/dev/tty",...) should work.

    But this isn't a very polite thing to do, because stdout might have been redirected by the user.

    Another idea would be something like this:

    FILE *myfile;
    FILE *datafile = fopen("data.txt"...);

    Then do

    myfile = stdout;
    myfile = datafile;

    (filp back and forth as often as you like)

    and do all your I/O to myfile.
    Last edited: May 15, 2007
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: Freopen() in UNIX
  1. Unix / scripting help (Replies: 3)