473,785 Members | 2,807 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3305
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.m s-windows.program mer.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(,,_IONB F,) at startup.

- formerly david.thompson1 || achar(64) || worldnet.att.ne t
Sep 1 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
17395
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 of code. it doesnt actually work.. but it is how I envisioned it:: if((n=accept(s,(struct sockaddr *)&remote,&sz))==-1) {printf("accept\n"); exit(0);} close(0); close(1); close(2); pipe(&i);
19
6810
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 what happens in memory operations, everyone knows the importance of freeing the allocated memory, but there do have memory leaks from time to
5
2734
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 called with an in-use file). However, what happens is that a copy of the file is made, and the old file still exists. For example: C:\>c:\python24\python.exe Python 2.4.2 (#67, Sep 28 2005, 12:41:11) on win32
17
14788
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
2507
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 line does, but I dont understand how to use dup
20
7308
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 stream so that the tape driver will write a filemark on the tape. Otherwise multiple clumps of data saved to tape would all appear to be one big file on the tape. When the tape unit device was explicitly opened with fopen() that's possible:...
4
6467
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 through stdin that way a bourne shell script I wrote will execute
1
5760
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... So say and update updates TABLE_A and I stop it, whilst this transaction is rolling back I attempt to rename TABLE_A to TABLE_A_OLD and rename a different table to become TABLE_A. I am assuming that the rollback actions will use the object...
53
4484
by: Bartc | last post by:
This short program: #include <stdio.h> #include <stdlib.h> int main(void) { int status; status=fclose(0);
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10095
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8978
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5383
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2881
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.