Hi, I'm a beginner in C++ (teaching myself basically) and I was wondering how to create a directory using C++ as I have discovered that ofstream won't create a file if the directory it is to be created in doesn't exist.
Printable View
Hi, I'm a beginner in C++ (teaching myself basically) and I was wondering how to create a directory using C++ as I have discovered that ofstream won't create a file if the directory it is to be created in doesn't exist.
there is the mkdir function in dir.h though this is compiler specific.
MSVC++ doesn't seem to have dir.h, but you could try direct.h
I can't find any documentation for it, but a quick browse of the file listed some functions that may be what you need.
>documentation for it
www.msdn.microsoft.com
PHP Code:
Example
/* MAKEDIR.C */
#include <direct.h>
#include <stdlib.h>
#include <stdio.h>
int main( void )
{
if( _mkdir( "\\testtmp" ) == 0 )
{
printf( "Directory '\\testtmp' was successfully created\n" );
system( "dir \\testtmp" );
if( _rmdir( "\\testtmp" ) == 0 )
printf( "Directory '\\testtmp' was successfully removed\n" );
else
printf( "Problem removing directory '\\testtmp'\n" );
}
else
printf( "Problem creating directory '\\testtmp'\n" );
return 0;
}
Output
Directory '\testtmp' was successfully created
Volume in drive C is CDRIVE
Volume Serial Number is 0E17-1702
Directory of C:\testtmp
05/03/94 12:30p <DIR> .
05/03/94 12:30p <DIR> ..
2 File(s) 0 bytes
17,358,848 bytes free
Directory '\testtmp' was successfully removed
Much appreciated.
i have a very very very very much muhco much mucho easier way to do that.
and the include file is process.hCode:system("mkdir directory/path/here");
True. That is an easy way, but I seem to see people shy away from using "system". Perhaps because it's not portable. There may be better reasons too...I can't remember, but it's allways been in my mind not to use system. (I think it's supposed to be much slower too).
>i have a very very very very much muhco much mucho easier way to do that
Yes, but the system function is terribly slow. Even for people who don't even consider efficiency it is slow enough to cause hesitation.
>and the include file is process.h
process.h is a nonstandard header file, you can also find system in cstdlib, or iostream which sometimes includes cstdlib.
>Perhaps because it's not portable.
This is true, but working with directories is also not portable so there's no real problem there.
-Prelude