The fastest and easiest:
int fd = open("file", O_WRONLY); off_t size = lseek(fd, 0, SEEK_END); ftruncate(fd, 0); ftruncate(fd, size);
Obviously, it would be nice to add some error checking.
This solution is not , however, what you want for safe file destruction. It will simply mark the old blocks used by the file as unused and leave a sparse file that does not take up any physical space. If you want to clear the old contents of a file from physical media, you can try something like:
static const char zeros[4096]; int fd = open("file", O_WRONLY); off_t size = lseek(fd, 0, SEEK_END); lseek(fd, 0, SEEK_SET); while (size>sizeof zeros) size -= write(fd, zeros, sizeof zeros); while (size) size -= write(fd, zeros, size);
You can increase the size of zeros to 32768 or so, if testing shows that it improves performance, but for a certain point it should not help and just be a waste.
R .. source share