473,480 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

fclose(stdin);

Hi!

Is there any harm/advantage to closing the standard input stream if I am not
going to read from it again?

I have:

if(blah)
fileptr=fopen(blah);
else
fileptr=stdin;

stuff;

if(fileptr!=stdin) /*this line*/
fclose(fileptr);

Can I just miss out the marked line and close whatever fileptr is?

Also, assuming I don't need the stream again, (and I can be reasonably sure
no other process will want it in the mean time) what reason is there to
close the file at all, as exit() will eventually close all open streams
anyway?
Nov 13 '05 #1
2 5846
On Sat, 19 Jul 2003 15:45:17 -0400, viza wrote:
Hi!

Is there any harm/advantage to closing the standard input stream if I am
not going to read from it again?
There is no harm and there is an advantage if you would like your
program to use as little memory as possible or file handles are in high
demand. In certain environments it is standard practice to close stdin,
stderr, and stdout so that the program can run as a background process
without confusing the parent process from which the program was spawned.
Also, assuming I don't need the stream again, (and I can be reasonably
sure no other process will want it in the mean time) what reason is
there to close the file at all, as exit() will eventually close all open
streams anyway?


Because it's a memory leak. If your program is short lived and opens
only a few files then I suppose memory leaks are ok. But I never write
code like that (try not to) just on principle.

Mike
Nov 13 '05 #2
in comp.lang.c i read:
On Sat, 19 Jul 2003 15:45:17 -0400, viza wrote:

Is there any harm/advantage to closing the standard input stream if I am
not going to read from it again?


There is no harm and there is an advantage if you would like your
program to use as little memory as possible or file handles are in high
demand. In certain environments it is standard practice to close stdin,
stderr, and stdout so that the program can run as a background process
without confusing the parent process from which the program was spawned.


actually this has subtle problems. an issue for a different group to
discuss.

--
a signature
Nov 13 '05 #3

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

Similar topics

0
5815
by: Bernhard Kuemel | last post by:
Hi! I want to read/write commands and program input to/from /bin/bash several times before I close the stdin pipe. However, reading from cat hangs unless I first close the stdin pipe. <?php...
6
24845
by: Phil Powell | last post by:
In TCL it would be written this way: I guess what I need is the PHP equivalent of TCL's gets command (see http://www.astro.princeton.edu/~rhl/Tcl-Tk_docs/tcl8.0a1/gets.n.html for more...
0
496
by: viza | last post by:
Hi! Is there any harm/advantage to closing the standard input stream if I am not going to read from it again? I have: if(blah) fileptr=fopen(blah); else
2
5620
by: Steve | last post by:
Is it allowed to fclose stdin? Trying it here it seems to work ok, but it occurs to me that on some other system the standard library might call fclose(stdin) during its clean up, and calling...
6
2455
by: Charlie Zender | last post by:
Hi, I have a program which takes the output filename argument from stdin. Once the program knows the output filename, it tries to open it. If the output file exists, the program asks the user to...
20
7241
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...
31
4022
by: Nikos Chantziaras | last post by:
Hello. Is there a way to check if the current process has an stdin handle? In the win32 API, one can do: _eof(_fileno(stdin)) Crucial here is that the above doesn't block. Is there a...
2
4022
by: kimonp | last post by:
I am running on windows XP with a fresh install of wamp5 (1.7.2) and mediawiki. I am trying to call a perl function from within php using proc_open. The perl script is being executed and...
53
4386
by: Bartc | last post by:
This short program: #include <stdio.h> #include <stdlib.h> int main(void) { int status; status=fclose(0);
3
3285
by: wrenashe | last post by:
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,...
0
7045
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,...
1
6741
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...
0
6944
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5341
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,...
1
4782
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4483
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...
0
2985
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1300
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 ...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.