added O_TRUNC to open so the file would be erased before written into