Thread: Directory Creation in C++

  1. #1
    Unregistered
    Guest

    Directory Creation in C++

    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.

  2. #2
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    there is the mkdir function in dir.h though this is compiler specific.
    -

  3. #3
    Registered User DeadArchDown's Avatar
    Join Date
    Apr 2002
    Posts
    28
    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.
    -------------------
    "Exception"

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    >documentation for it

    www.msdn.microsoft.com

    PHP Code:
    Example

    /* MAKEDIR.C */

    #include <direct.h>
    #include <stdlib.h>
    #include <stdio.h>

    int mainvoid )
    {
       if( 
    _mkdir"\\testtmp" ) == )
       {
          
    printf"Directory '\\testtmp' was successfully created\n" );
          
    system"dir \\testtmp" );
          if( 
    _rmdir"\\testtmp" ) == )
            
    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 
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    Registered User DeadArchDown's Avatar
    Join Date
    Apr 2002
    Posts
    28
    Much appreciated.
    -------------------
    "Exception"

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    206
    i have a very very very very much muhco much mucho easier way to do that.

    Code:
    system("mkdir directory/path/here");
    and the include file is process.h

  7. #7
    Registered User DeadArchDown's Avatar
    Join Date
    Apr 2002
    Posts
    28
    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).
    -------------------
    "Exception"

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Couple errors please help :-D
    By JJJIrish05 in forum C Programming
    Replies: 9
    Last Post: 03-06-2008, 02:54 AM
  3. Newton + Einstein were wrong!
    By Jez in forum A Brief History of Cprogramming.com
    Replies: 64
    Last Post: 12-14-2004, 02:24 PM
  4. Replies: 6
    Last Post: 07-30-2003, 03:08 AM
  5. Directory reading trouble
    By samGwilliam in forum Linux Programming
    Replies: 0
    Last Post: 03-10-2002, 09:43 AM