What happens if i dont use fclose




















However, if the file is being used for output, some data may still be buffered and not physically written to the output file. It is for this reason that it is important to explicitly call fclose so that any buffered data is written to the file. Failing to properly close a file used for output can result in data loss - this is the "unexpected error" that you should expect!

Within a single program executing a single time, once a file is opened it can be accessed as needed. If you fail to close the file before the program terminates, the operating system will close the file as part of its housekeeping function.

The file can then be opened again. Originally Posted by kmess. Last edited by CommonTater; at AM. At normal program exit either via a return from the initial call to main, or if exit is called , files are guaranteed to be closed properly, which includes flushing. The main desktop operating systems will do that, but it's not guaranteed. Could you provide a reference, why wouldn't this be OS specific? MatteoItalia, thanks, but it's also true that if the process terminates unexpectedly for some other reason, then most modern OS's will clean up the handles and memory anyway.

Peter: sure thing, but, since you wanted an OS-agnostic reference of this fact, I provided only the guarantees given by the standard. Jonathan Leffler Jonathan Leffler k gold badges silver badges bronze badges. The standard says that "Whether open streams with unwritten buffered data are flushed, open streams are closed, or temporary files are removed is implementation-defined.

The streams are not closed; the file descriptors are. There's a big difference. Peter Peter 3, 20 20 silver badges 32 32 bronze badges. A typical modern operating system will do that, at least. My understanding is that back in the day, Windows 3. And there may be some other very minimal OS's these days embedded systems, perhaps that don't handle that for you. Good point; though as you say above, the CRT doesn't guarantee that handles will be closed if the application exits abnormally.

It's typically the OS that does that - and it's my understanding that there are some very old or very specialized operating systems that don't make that guarantee. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Does ES6 make JavaScript frameworks obsolete? Podcast Do polyglots have an edge when it comes to mastering programming Featured on Meta.

Now live: A fully responsive profile. Linked 3. See more linked questions. Stephen on 4 Feb Vote 2. Cancel Copy to Clipboard. Edited: Stephen on 4 Feb Short Answer:. Files saved on a drive are not instantly accessible at any moment: to read or write to a file, the OS has to create a handle to it: creating a file handle has overhead and requires system resources.

If you do not close files, then these file handles will accumulate, waste system resources, and eventually can crash any application trying to access files e. When you fclose the file then those system resources are released.

Longer Answer:. Use the internet. When I searched for "why files open close" using [a major internet search engine] it returned million results. The first ones that I looked at gave good overviews, and some gave more detailed explanations. Be prepared for learning a lot about how OS's work! Guillaume on 4 Feb Within the contest of matlab, we can summarise it as follow:. More Answers 0.



0コメント

  • 1000 / 1000