![]() |
|
#1
|
|||
|
|||
??? 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
|
|||
|
|||
Re: ??? if no fclose()Quote:
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
|
||||
|
||||
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
|
|||
|
|||
Re: ??? if no fclose()thank you mr.dave and thank you mr.walt.
__________________
balu------>>>>>U perseverance pays slowly. |
Recent GIDBlog
Writing a book by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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