Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fopen without fclose in C

Tags:

c

file

file-io

What happens if i open a file using fopen some n number of times without calling fclose on it?

Any buffer overrun issues may arise?

like image 503
chaitanyavarma Avatar asked Jul 28 '10 13:07

chaitanyavarma


4 Answers

If you continue to fopen without fclose then eventually your future calls to fopen will start to fail. There are a limited number of file descriptors available to your program.

See this related SO question.

like image 88
Brian R. Bondy Avatar answered Nov 13 '22 17:11

Brian R. Bondy


You waste underlying file handles. Please close any files you open, in a timely fashion, as to avoid this leak of resources.

like image 38
NT_ Avatar answered Nov 13 '22 16:11

NT_


If you continue opening files without closing them then you will run out of file descriptors at some point, either at the application level or the OS level, and all further attempts to open a file will fail.

like image 3
Ignacio Vazquez-Abrams Avatar answered Nov 13 '22 17:11

Ignacio Vazquez-Abrams


Aside from wasting file descriptors for the process as others answered, you would also waste memory since each file stream manages in/out buffers that are allocated internally by libc.

like image 3
Nikolai Fetissov Avatar answered Nov 13 '22 16:11

Nikolai Fetissov