freopen, fclose and stdout

Discussion in 'C Programming' started by Alex Vinokur, Dec 3, 2003.

  1. Alex Vinokur

    Alex Vinokur Guest

    Hi,


    freopen closes stdout, but it seems that fclose doesn't restore it (see sample below).

    How can one print to stdout after using freopen?


    ========= C code : File foo.c : BEGIN =========
    #include <stdio.h>
    #include <assert.h>

    #define FILE_NAME "foobar"

    int main ()
    {
    FILE* out_fp;
    int rc;

    remove (FILE_NAME);

    printf ( "printf-1\n");

    out_fp = freopen (FILE_NAME, "a", stdout);
    assert (out_fp);

    printf ("printf-2\n");
    fprintf (out_fp, "fprintf(out_fp)\n");

    rc = fclose (out_fp);
    assert (!rc);

    printf ("printf-3\n"); // Problematic printf

    return 0;
    }
    ========= C code : File foo.c : END ===========


    ========= Compilation & Run : BEGIN =========

    $ gcc -v
    [---omitted---]
    gcc version 3.3.1 (cygming special)

    $ gcc -W -Wall foo.c

    $ a
    printf-1
    // printf-3 missing

    $ cat foobar
    printf-2
    fprintf(out_fp)

    ========= Compilation & Run : END ===========



    =====================================
    Alex Vinokur
    mailto:
    http://up.to/alexvn
    =====================================
    Alex Vinokur, Dec 3, 2003
    #1
    1. Advertising

  2. Alex Vinokur

    Grumble Guest

    Grumble, Dec 3, 2003
    #2
    1. Advertising

  3. Alex Vinokur

    Eric Sosman Guest

    Alex Vinokur wrote:
    >
    > Hi,
    >
    > freopen closes stdout, but it seems that fclose doesn't restore it (see sample below).
    >
    > How can one print to stdout after using freopen?


    This is Question 12.34 in the comp.lang.c Frequently
    Asked Questions (FAQ) list

    http://www.eskimo.com/~scs/C-faq/top.html

    .... and you're not going to like the answer.

    --
    Eric Sosman, Dec 3, 2003
    #3
  4. Alex Vinokur

    schneidp20

    Joined:
    Jan 24, 2009
    Messages:
    1
    I couldn't take no for an answer and dug a little deeper. At least for MS systems you can switch back and forth from stdout and redirected stdout:

    printf("To Console\n");
    f = freopen("out.txt","w",stdout);
    printf("redirect to file\n");
    f = freopen("CON", "w", stdout);
    printf("back to console\n");

    Enjoy!
    David Schneider
    schneidp20, Jan 24, 2009
    #4
  5. Alex Vinokur

    vidyaani

    Joined:
    Jan 11, 2013
    Messages:
    1
    How to print the stdout after using freopen..

    Solution:

    you can type these freopen ("/dev/tty", "a", stdout).
    before that printf statement which you want to print in the stdout
    Is this what you were trying to do :)
    vidyaani, Jan 11, 2013
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Vinokur

    freopen & cout

    Alex Vinokur, Dec 2, 2003, in forum: C++
    Replies:
    1
    Views:
    1,191
    Jack Klein
    Dec 3, 2003
  2. tmpfile and freopen

    , Jun 5, 2006, in forum: C Programming
    Replies:
    1
    Views:
    364
    Barry Schwarz
    Jun 15, 2006
  3. David Mathog

    fclose then fopen equivalent for stdout?

    David Mathog, Oct 25, 2006, in forum: C Programming
    Replies:
    20
    Views:
    2,005
    Jordan Abel
    Oct 27, 2006
  4. Pietro Cerutti

    safe to fclose stdout to avoid memory leak?

    Pietro Cerutti, Aug 20, 2007, in forum: C Programming
    Replies:
    7
    Views:
    373
    Antoninus Twink
    Aug 30, 2007
  5. Guillaume Dargaud

    freopen on both stdout and stderr

    Guillaume Dargaud, Nov 23, 2007, in forum: C Programming
    Replies:
    6
    Views:
    2,103
    Charlie Gordon
    Nov 26, 2007
Loading...

Share This Page