How can I generate a unique filename or path? I am looking for a solution that works both for Unix(-like) operating systems as well as Windows.
Use boost::uuid
:
#include <string>
using std::string;
#include <boost/lexical_cast.hpp>
using boost::lexical_cast;
#include <boost/uuid/uuid.hpp>
using boost::uuids::uuid;
#include <boost/uuid/uuid_generators.hpp>
using boost::uuids::random_generator;
#include <boost/uuid/uuid_io.hpp>
string make_uuid()
{
return lexical_cast<string>((random_generator())());
}
And use the generated UUID as the filename (the path is therefore irrelevant).
Use mktemp on both. Windows have got a version of the posix call at msdn docs and the linux call is mktemp(3). mktemp command lines tool on linux incidentally just calls that
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With