I'm not sure what is standard practice, but should the temp file be located in the same directory (and therefore same physical filesystem) as the intended output file?  Otherwise you need to write the file twice.