473,382 Members | 1,563 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,382 software developers and data experts.

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 5835
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
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
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
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
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
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
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
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
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
by: Bartc | last post by:
This short program: #include <stdio.h> #include <stdlib.h> int main(void) { int status; status=fclose(0);
3
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,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.