tags:

views:

130

answers:

3

Hi,

How can i create a temporary folder in tmp folder using C++ language.

Edited: This is my code but is showing an error. Actually, i have 3 volumes. Leopard, Development and 10.6 (in Mac OS X) and i want to create a temp directory in current home directory. Here is my code,

// i am confused about this line char* tempdir = "/Volumes/Development/NewFolder.XXXXXX";

if (!mkdtemp(tempdir))

fprintf(stderr, "Not able to create directory");

+4  A: 

You can use the boost::Filesystem library function: create_directory( "temp" );

This is very portable and will work under most operating systems.

Boost can be downloaded here.

bitc
+1, I should have thought of that too. :)
avakar
+1...which is why OP needed to clarify which language is being used.
Amardeep
+4  A: 

Under POSIX, you can use mkdtemp to create a directory with a unique name. On Windows, use GetTempPath to retrieve the name of the temp directory, then create a directory with a random name there.

avakar
Can't get simpler than this :)
bitc
@avatar Thanks guys for this information...
iSight
@iSight, no problem, though what you used is not my nickname. One day, Cameron will feel my wrath.
avakar
@avakar This is my code but is showing an error. Actually, i have 3 volumes. Leopard, Development and 10.6 (in Mac OS X) and i want to create a temp directory in current home directory.Here is my code,// i am confused about this linechar* tempdir = "/Volumes/Development/NewFolder.XXXXXX"; if (!mkdtemp(tempdir)) { fprintf(stderr, "Not able to create directory"); }
iSight
A: 

Hello iSight,

Boost is an excellent choice, but one of the problems with boost is that you would probably be downloading a huge amount of sources - if all you need is the filesystem functionality this is a bit of an overkill. Try http://stlplus.sourceforge.net/stlplus3/docs/file_system.html

Also why don't you just use the good old system() function? The string argument to system of course would be platform dependent.

Arpan

Fanatic23