Tag: clear

在“r +”模式下打开时,在C中清除/截断文件

我的代码目前看起来像这样(这些步骤分为多个函数): /* open file */ FILE *file = fopen(filename, “r+”); if(!file) { /* read the file */ /* modify the data */ /* truncate file (how does this work?)*/ /* write new data into file */ /* close file */ fclose(file); } 我知道我可以在”w”模式下打开文件,但在这种情况下我不想这样做。 我知道在unistd.h / sys/types.h有一个函数ftruncate ,但是我不想使用这些函数,我的代码应该是高度可移植的(在windows上也是如此)。 是否有可能在不关闭/重新打开文件的情况下清除文件?