用open()写入之前清除文件

使用fopen ,通过将其设置为w将自动为我清除文件。 但是现在我正试图用open做同样的事情,

 int file = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); 

这不保证文件为空并在开头写(?)。 我怎样才能做到这一点?

添加O_TRUNC – 最初清除文件中的所有数据。

 O_TRUNC flag truncates the file when opening. It can be used. int file = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);