Unix-systems allow you to delete open files (or rather, delete all links to a file from the file system). But the file descriptor is still valid. Any read and write requests will succeed, as they will be with the file name.
In other words, you cannot completely delete a file until the file descriptor is closed. After closing, the file is automatically deleted.
With a valid file descriptor, you can check if a file name still exists, e.g.
printf("%d\n", buf.st_nlink); // 0 means no filenames
Where bufis struct stat, initialized fstat.
source
share