Guaranteed file deletion upon program termination (C/C++)

前端 未结 8 1274
长发绾君心
长发绾君心 2020-12-14 02:16

Win32\'s CreateFile has FILE_FLAG_DELETE_ON_CLOSE, but I\'m on Linux.

I want to open a temporary file which will always be deleted upon program terminat

8条回答
  •  情书的邮戳
    2020-12-14 03:12

    If you're just making a temporary file, just create it in /tmp or a subdirectory thereof. Then make a best effort to remove it when done through atexit(3) or similar. As long as you use unique names picked through mkstemp(3) or similar even if it fails to be deleted because of a program crash, you don't risk reading it again on subsequent runs or other such conditions.

    At that point it's just a system-level problem of keeping /tmp clean. Most distros wipe it on boot or shutdown, or run a regular cronjob to delete old files.

提交回复
热议问题