473,597 Members | 2,459 Online
Bytes | Software Development & Data Engineering Community
+ 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(b lah);
else
fileptr=stdin;

stuff;

if(fileptr!=std in) /*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 5862
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
5836
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 $descriptorspec = array( 0 => array("pipe", "r"), // stdin is a pipe that the child
6
24854
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 information) or perhaps the PHP equivalent of how a Java app would read from standard in:
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
5643
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 fclose twice (on normal streams at least) gives undefined behaviour.
6
2473
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 confirm whether he really wants to overwrite the existing output file. The problem is that the second read from stdin, to obtain the user response whether to overwrite the existing output file, never waits for the user's response. It's as if a...
20
7276
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:...
31
4048
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 standard way to do the same without resorting to OS-specific API calls?
2
4031
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 returns a successful exit value, and I can read the results it prints to STDOUT in PHP, but the perl script does not see STDIN from PHP. Since I want to use the perl script as a filter and I don't want to create an intermediary file, this is problematic....
53
4427
by: Bartc | last post by:
This short program: #include <stdio.h> #include <stdlib.h> int main(void) { int status; status=fclose(0);
3
3293
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, fopen(A); 7. dup2(A, stdout); 8. dup2(A, stderr);
0
7971
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
7893
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8276
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...
0
8381
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8259
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5847
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5436
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
3932
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2408
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

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.