Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: dup2, fclose and rename ...

Reply
Thread Tools

Re: dup2, fclose and rename ...

 
 
David Thompson
Guest
Posts: n/a
 
      09-01-2008
On Wed, 20 Aug 2008 03:05:32 -0700 (PDT), wrenashe
<(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
freopen, fclose and stdout Alex Vinokur C Programming 4 01-11-2013 07:22 AM
fopen and fclose? kathy C++ 17 02-15-2006 06:14 PM
Segmentation Fault in fclose()...why? VB C++ 3 01-15-2005 04:26 PM
fclose() causing access violation in threadFunc Anoop Nair C++ 1 08-05-2004 06:57 PM
fclose(stdin); viza C Programming 0 07-19-2003 07:45 PM



Advertisments