А причём здесь C++?
Вообще, вместо tempnam нужно использовать mkstemp.
Вот отрывок из man tempnam:
BUGS
The precise meaning of `appropriate' is undefined; it is unspecified how accessibility of a directory is determined. Never use this
function. Use mkstemp(3) instead.
Это insecure. Появляется time window между получением имени временного файла и его открытием. Так что нужно использовать mkstemp, а затем привязывать файловый дескриптор к fstream, если необходимо.
Читайте внимательнее:
DESCRIPTION
The mkstemp() function generates a unique temporary file name from
template. The last six characters of template must be XXXXXX and these are replaced with a string that makes the filename unique.