Posted by: sureshamrita | August 9, 2010

using tmpnam is dangerous. Use mkstemp

Whenever I used tmpnam my g++ compiler cribbed that its dangerous to use tmpnam and must use mkstemp. But mkstemp returned file id and as my language is C++, i never knew how to use the C file ids with C++ fstreams. Then I found out from the man page of fstream that the following can be done!,

char *tmpFileName= strdup("/tmp/tmpfileXXXXXX");
if(mkstemp(tmpFileName) == -1)
throw runtime_error("temporary file cannot be created");
ofstream out(tmpFileName);

Enjoy C++

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: