write to file when opened file closes.