Writing to a Posix File
If you happen to write to a file on a Linux/Unix/Posix system, then you might be tempted to open the file with O_WRONLY | O_CREAT.
I’m here to warn you that you probably want O_WRONLY | O_CREAT | O_TRUNC
. Noticing this mistake took me an embarrassing amount of time to track down.
O_TRUNC
What this flag does for a file is tell the operating system to truncate the file when opening. If you don’t do this, the file may be filled with the data from any previous files with the same name. The nature of this type of bug inside a larger build process makes it somewhat hard to analyze, reproduce, and fix. Just get in the habit of asking for truncated files and save yourself the headache.