use defer to close the file once open