Skip to content

Instantly share code, notes, and snippets.

@mattyclarkson
Created April 4, 2013 09:37
Show Gist options
  • Save mattyclarkson/5309098 to your computer and use it in GitHub Desktop.
Save mattyclarkson/5309098 to your computer and use it in GitHub Desktop.
A C++11 routine to create a safe temporary file name.
const constexpr SizeT junk = 'z' - 'a';
Char buffer[L_tmpnam + junk] = {'\0'};
// cppcheck-suppress obsoleteFunctionstmpnam
std::tmpnam(buffer);
SizeT len = std::strlen(buffer);
std::random_device rd;
for (SizeT i = len; i < (L_tmpnam + junk - 1); ++i) {
buffer[i] = std::max('a', std::min<Char>('z',
'a' + (i - len) + (rd() & 0xF)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment