Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erlang, Is it necessary to close the file here?

Tags:

erlang

When using file:read_file(x) is it necessary to close the returned file?

{ok, File} = file:read_file("maillog.sample"), 
file:close(File),
like image 398
jdc0589 Avatar asked Jan 08 '10 05:01

jdc0589


1 Answers

It is not a file, but the contents of the file that is returned. Thus, no file to close. Try changing the variable name to Data or similar as in the code below:

{ok, Data} = file:read_file("maillog.sample"),

Data will then have the contents of the file "maillog.sample". The function file:read_file/1 will open, read and close the file for you, all in one go.

like image 187
Koistinen Avatar answered Sep 19 '22 08:09

Koistinen