Safe file opening for write