Is there a portable way of creating temporary files with C++ that get automatically deleted when the program terminates (regardless of whether it crashes, gets killed, or just reaches return 0;
in main()
.).
On Unix systems, I can open a file, delete it and then keep the still existing handle. This works with FILE *
, std::fstream
etc.
On Windows, this appears not to work. The only way I found is using CreateFile
with the FILE_FLAG_DELETE_ON_CLOSE
flag.
Is there something smarter that (1) works both on Linux and Windows, (2) has the "the file is removed on when the program terminates" behaviour as on Linux. I would be fine with #ifdef
code as long as the file type that I work with is the same on both systems (e.g. std::fstream
or FILE *
).
I know about this solution, but this appears only to work on graceful exits and would require me to either set up central handlers and manage all temporarily opened files.
Edit: Rephrased the question to "how can I get files on Windows that are automatically removed as removed-but-still-open files in Linux.