write file function