Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tempnam equivalent in C++

Tags:

c++

c

I need to generate random names which I'll be using to create temporary files in a directory. Currently I am using C standard function tempnam() for this. My code is in C++ and would like to use C++ equivalent for doing the same task. The code needs to work on Solaris as well as on Windows.

Is anyone aware of such thing in C++? Any pointer on this would be highly appreciated.

like image 817
rockoder Avatar asked Jun 30 '26 18:06

rockoder


1 Answers

Try std::tempnam in the cstdio header. ;)

The C standard library is still available in C++ code. For convenience, they provide C++ wrappers (in headers with the 'c' prefix, and no extension), and available in the std namespace.

You can also use the plain C version (stdio.h and tempnam in the global namespace, but you did ask for the C++ version ;))

The C++ standard library only provides new functions when there's actually room for improvement. It has a string class, because a string class is an improvement over char pointers as C has. It has a vector class, because, well, it's useful.

For something like tempnam, what would C++ be able to bring to the party, that we didn't already have from C? So they didn't do anything about it, other than making the old version available.

like image 65
jalf Avatar answered Jul 03 '26 08:07

jalf