GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Jul-2006, 09:36
balusss balusss is offline
Junior Member
 
Join Date: Dec 2005
Posts: 64
balusss is on a distinguished road
Question

??? if no fclose()


could somebody pls tell me , what exactly happens if we don't close the file pointers properly? none of my small programs really does seem to bother about it, but i want to know what happens in the back ground and what possible loss it may force upon me once i start coding big programs, or for that matter, code size is a matter at all or not??? does the compiler have a built-in ability to handle it?
__________________
balu------>>>>>U
perseverance pays slowly.
  #2  
Old 06-Jul-2006, 10:54
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: ??? if no fclose()


Quote:
Originally Posted by balusss
could somebody pls tell me , what exactly happens if we don't close the file pointers properly? none of my small programs really does seem to bother about it, but i want to know what happens in the back ground and what possible loss it may force upon me once i start coding big programs, or for that matter, code size is a matter at all or not??? does the compiler have a built-in ability to handle it?

The answer to "what exactly happens" is, unfortunately, "It depends."

For both Standard C and Standard C++ programs, a "normal" program exit (executing return; from main(), or executing exit(n); from almost anywhere) closes all open streams (and, therefore, flushes all output buffers).

One potential "gotcha": in a C++ program, if exit(n) is called from a destructor of an object that has static storage duration, the result is indeterminate. (But I'm not sure that calling exit() from a destructor is a particularly Good Thing anyhow.)

In polite society, it is generally considered "good programming practice" to close all streams when exiting a program. (And, a reasonable defensive programming technique, to protect yourself from some compilers that may not completely comply with all standards.)

In other words: why would you not close all of your program's streams? You, the programmer, should know exactly what your program is doing, including what streams are open. Why not be neat and tidy. If, through absent-mindedness or just plain laziness you neglect to clean up after yourself, I am automatically suspicious; wondering what other stuff you "forgot" to do. (But that's just a silly little quirk of mine, and is a matter of style, not substance.)

If, at any time during the execution of the program, a stream is no longer being used, why not just close it then and there? If a program crashes, rather than performing a "normal" exit, you will (probably) lose whatever is in the output buffers of open streams. In some cases, even if the output buffer has already been flushed, an output stream can be corrupted by an abnormal program exit.

Regards,

Dave
Last edited by davekw7x : 06-Jul-2006 at 11:34.
  #3  
Old 06-Jul-2006, 11:07
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: ??? if no fclose()


For small programs, nothing really. When the program exits, the files are closed.

But when you start making larger programs, you may try to open a file you already opened earlier and the open will fail. So get in the habit of closing your files as soon as you don't need tham anymore.

Do not assume the compiler has built-in ability to handle anything you misuse. Some compilers are forgiving, others are not. If you learn on a forgiving compiler and use sloppy techniques, you will have a hard time programming on a compiler that's not sloppy.
__________________

Age is unimportant -- except in cheese
  #4  
Old 07-Jul-2006, 02:48
balusss balusss is offline
Junior Member
 
Join Date: Dec 2005
Posts: 64
balusss is on a distinguished road

Re: ??? if no fclose()


thank you mr.dave and thank you mr.walt.
__________________
balu------>>>>>U
perseverance pays slowly.
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
using vector or array oshiotse C++ Forum 4 16-Apr-2004 10:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:31.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.