Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I delete a file upon its close in C++ on Linux?

I wish for a file to be deleted from disk only when it is closed. Up until that point, other processes should be able to see the file on disk and read its contents, but eventually after the close of the file, it should be deleted from disk and no longer visible on disk to other processes.

like image 591
WilliamKF Avatar asked Feb 04 '23 03:02

WilliamKF


1 Answers

Open the file, then delete it while it's open. Other processes will be able to use the file, but as soon as all handles to file are closed, it will be deleted.

Edit: based on the comments WilliamKF added later, this won't accomplish what he wants -- it'll keep the file itself around until all handles to it are closed, but the directory entry for the file name will disappear as soon as you call unlink/remove.

like image 58
Jerry Coffin Avatar answered Feb 06 '23 15:02

Jerry Coffin