473,218 Members | 1,435 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,218 software developers and data experts.

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 3260
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: PC | last post by:
Is it possible to create pipe()s (stdin/stdout) for a child process and forking a shell with execl() and controlling the shell's stdin/stdout from the parent with select()? Heres a little snippet...
19
by: lihua | last post by:
Hi, Group! I got one question here: We all know that fclose() must be called after file operations to avoid unexpected errors.But there are really cases when you forget to do that!Just like...
5
by: Tony Meyer | last post by:
On Windows, if I do os.rename(old, new) where old is a file that is in-use (e.g. python itself, or a dll that is loaded), I would expect that an error would be raised (e.g. as when os.remove is...
17
by: kathy | last post by:
if fopen failed, does it necessary to call fclose? I see an example like this: .... stream = fopen(...); if(stream == NULL) { .... } else
2
by: seifyr | last post by:
What sequence of calls to open, dup and/or dup2 do you perform in your program in order to achieve the following effects? $prog1>file 2>&1 and $prog2 2>&1 > file I understand which each...
20
by: David Mathog | last post by:
A program of mine writes to a tape unit. Output can be either through stdout or through a file opened with fopen(). When all the data is transferred to tape the program needs to close the output...
4
by: DyslexicAnaboko | last post by:
Hello, I have a module that is part of larger project that is giving me trouble, so I setup an example. Brief ===== I simply want to open a text file and make the contents avaliable...
1
by: cheesey_toastie | last post by:
I have a long query which I have set off and would like to stop, and rename one of the tables used. My query is due to my lack of understanding of the underlying structure of MSSQL-Server... ...
53
by: Bartc | last post by:
This short program: #include <stdio.h> #include <stdlib.h> int main(void) { int status; status=fclose(0);
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.