added check for closed file pointer