Re: dup2, fclose and rename ...

Discussion in 'C Programming' started by David Thompson, Sep 1, 2008.

  1. On Wed, 20 Aug 2008 03:05:32 -0700 (PDT), wrenashe
    <> wrote:

    > I am on windows 2003, basically my codes want to do such a thing:
    >
    > 1. fopen a file A.
    > 2. dup2(A, stdout);
    > 3. dup2(A, stderr);
    > 4. fclose(A);


    You can't mix those two different levels -- standard C I/O (fopen,
    etc.) uses FILE* pointers including stdin,out,err lowercase, versus
    'low-level' POSIX-but-not-C I/O uses small integer file-descriptors
    including STDIN,OUT,ERR uppercase (which at least the mingw version of
    win32 tweaks). Standard C I/O cannot portably cause streams to share
    an open (POSIX can with fdopen, but not for std* streams), but in C99
    (and perhaps C90 as an extension) you can redirect std* with freopen,
    which can achieve the goal of capturing output from existing code.

    If you change to lowlevel, you are offtopic in clc, but ...

    > 5. rename(A), and move it to somewhere else.
    > 6, fopen(A);
    > 7. dup2(A, stdout);
    > 8. dup2(A, stderr);
    > ..
    > ..
    >
    > While I found A can not be renamed after it is fclosed. On process
    > explorer, file A still gets two open handles. So any suggestions?
    >

    .... of course; if you A=open(name,) and dup2() to STDOUT and STDERR
    and close(A) only, STDOUT and STDERR are still open. They are
    different fd's, though for the same file, so you need to close each of
    them. And if you are using 'high-level' I/O, make sure it is flushed
    first -- or isn't buffered, e.g. setvbuf(,,_IONBF,) at startup.

    - formerly david.thompson1 || achar(64) || worldnet.att.net
    David Thompson, Sep 1, 2008
    #1
    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. PC

    pipe() dup2() fork() execl() select()

    PC, Aug 9, 2003, in forum: C Programming
    Replies:
    3
    Views:
    1,242
  2. Alex Vinokur

    freopen, fclose and stdout

    Alex Vinokur, Dec 3, 2003, in forum: C Programming
    Replies:
    4
    Views:
    8,333
    vidyaani
    Jan 11, 2013
  3. Help with open, dup and dup2

    , Mar 16, 2006, in forum: C Programming
    Replies:
    2
    Views:
    368
    Ian Collins
    Mar 16, 2006
  4. DyslexicAnaboko

    problems redirecting an open file to stdin using dup2()

    DyslexicAnaboko, Dec 5, 2006, in forum: C Programming
    Replies:
    4
    Views:
    778
    DyslexicAnaboko
    Dec 5, 2006
  5. Hongliang Wang

    dup2

    Hongliang Wang, Apr 22, 2014, in forum: C++
    Replies:
    14
    Views:
    130
    Hongliang Wang
    Apr 30, 2014
Loading...

Share This Page