When using file:read_file(x) is it necessary to close the returned file?
{ok, File} = file:read_file("maillog.sample"),
file:close(File),
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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:
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.