2

Possible Duplicate:
fopen without fclose in C

Why do I need to use fclose function in C? I do close the file every time I open it in c, still just for sake of knowledge i want to know what would happen if I do not use the fclose function? If anyone has faced problems by not using fclose please do share.

Community
  • 1
  • 1
Parth Shah
  • 573
  • 1
  • 6
  • 17
  • If you do not close the file, you do not have to open it again. Do you open it more than once? – alk Oct 12 '12 at 10:10
  • No that is not what i am talking about. I am talking about the scenario when i terminate the program without using **fclose**. Why use fclose, that is what i wanted to know. – Parth Shah Oct 12 '12 at 10:24
  • No @alk I do not have to open it more than once – Parth Shah Oct 12 '12 at 10:32

3 Answers3

6

If you do not close a file, you will run out of resources for more files. In Linux the default is 1024 open files per process, this is because a "file handle" is an index into a table (i.e. indexing an array). How it's handled on Windows I don't know, but sooner or later the process is going to run out of resource if you just keep on opening files but not closing them.

On the other hand, if you open a single file, that you use throughout the lifetime of the program, then there is no need to open/close it all the time. Open it once and leave it open. Also, all open files will be closed when exiting the process, so if your program just does some quick processing of a file and then exiting you don't have to close it. However, if I was reviewing the code I would still add a note about it, if you allocate a resource (memory, files, something else) I think you should release it when done with it even if it's done automatically by the system.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    So its just about developing a good habit which comes in handy when i am designing a code with large number of open files? It makes no difference for small files because the process is going to close them, right? – Parth Shah Oct 12 '12 at 10:16
  • Thanks! can you tell me how did you gain this knowledge? Can you suggest a few books/blogs that you have referred – Parth Shah Oct 12 '12 at 10:26
  • @ParthShah I wrote my first programs on a platform where the system _didn't_ free resources for you, so I learned the "hard way". :) – Some programmer dude Oct 12 '12 at 11:02
  • Ok got it. It depends on the system, better to use functions like **fclose** and **free** – Parth Shah Oct 13 '12 at 09:59
1

any file is i/o source, so similar to any source: after you worked with it, release it.
fclose function is used for that reason, which allows a the file to be used by another manager/processer etc.
If you dont use fclose function this might lead to the situation in which another process/manager can`t access given file

spin_eight
  • 3,925
  • 10
  • 39
  • 61
0

if you don't close the file is possible to not store all information on the memory. also the file can not be opended by somebody else

Adrian Herea
  • 658
  • 6
  • 7