|  | dup2, fclose and rename ... |  | |
| | | wrenashe |  |
| Posted: Wed Aug 20, 2008 10:05 am Post subject: 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. |
| |
| | | santosh |  |
| Posted: Wed Aug 20, 2008 10:05 am Post subject: Re: dup2, fclose and rename ... |  |
wrenashe wrote:
| Quote: | 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>. |
| |
| | | viza |  |
| Posted: Wed Aug 20, 2008 11:54 am Post subject: Re: dup2, fclose and rename ... |  |
Hi
On Wed, 20 Aug 2008 03:05:32 -0700, wrenashe wrote:
| Quote: | 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 |
| |
| | | David Thompson |  |
| Posted: Mon Sep 01, 2008 4:53 am Post subject: Re: dup2, fclose and rename ... |  |
| |  | |
On Wed, 20 Aug 2008 03:05:32 -0700 (PDT), wrenashe <wrenashe@gmail.com> wrote:
| Quote: | 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 ...
| Quote: | 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 |
| |
|
|