Re: dup2, fclose and rename ...
On Wed, 20 Aug 2008 03:05:32 -0700 (PDT), wrenashe
> 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
|All times are GMT. The time now is 08:48 PM.|
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.