Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate an unique filename or path?

Tags:

c++

unix

windows

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.

like image 710
pandreym Avatar asked Oct 14 '25 09:10

pandreym


2 Answers

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).

like image 67
hmjd Avatar answered Oct 16 '25 21:10

hmjd


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

like image 30
Osada Lakmal Avatar answered Oct 16 '25 23:10

Osada Lakmal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!