468,765 Members | 1,413 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,765 developers. It's quick & easy.

dup2, fclose and rename ...

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);
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?

Please, thanks.
Aug 20 '08 #1
3 2917
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);
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?
Post in <news:comp.os.ms-windows.programmer.win32>.

Aug 20 '08 #2
Hi

On Wed, 20 Aug 2008 03:05:32 -0700, 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);
It's probably not smart to mix calls like this. Perhaps try freopen()?

viza
Aug 20 '08 #3
On Wed, 20 Aug 2008 03:05:32 -0700 (PDT), wrenashe
<wr******@gmail.comwrote:
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
Sep 1 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by PC | last post: by
17 posts views Thread by kathy | last post: by
2 posts views Thread by seifyr | last post: by
20 posts views Thread by David Mathog | last post: by
4 posts views Thread by DyslexicAnaboko | last post: by
1 post views Thread by cheesey_toastie | last post: by
53 posts views Thread by Bartc | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.