vendredi 2 octobre 2020

How can I safely and portably use and delete a temporary file in C++11?

I'm writing a library. In one of my unit tests, I want to create a temporary file, make some library calls involving that file, and delete the file on exit (success, failure and exception - cleanup is important!).

Over 10 years ago, this was asked: How to create a temporary text file in C++? . But - the answers there are either non-portable, or not a proper complete solution (e.g. you get a file descriptor with no obvious way to get the name, so you can't access your file by name (std::tmpfile); you get a name (std::tmpnam) but can't be certain it's not already used; etc.) If that were resolved, I suppose my question would be answered by some RAII wrapper around some of these calls. But - I'm concerned with current reality.

In fact, my library must be usable with C++11, so I would prefer a C++11 solution if possible. Use of Boost is also acceptable but not desirable.

Aucun commentaire:

Enregistrer un commentaire