Thread: Creating a folder with subfolders

  1. #1
    Registered User
    Join Date
    Sep 2011

    Creating a folder with subfolders

    So I have been using mkdir() to make my folders, only problem is that it doesn't create subfolders.

    I.e. if mkdir("c:\a\b\c\d") but if c:\a\b\c are not present it doesn't make d folder.

    To circumvent this i made this code, it looks fine and runs fine but crashes at the end. No idea why though :/ Might have missed something silly.


    #include <iostream>
    #include <direct.h>
    using namespace std;
    int main()
        char file_loc[140] = "C:/a/b/c/d/";
        char tempfile_loc[140] = {' '};
        int counter = 0;
            while( file_loc[counter] != '/' )
                tempfile_loc[counter] = file_loc[counter];
            tempfile_loc[counter] = file_loc[counter];
            cout << tempfile_loc << endl;
            mkdir(tempfile_loc); // problem only makes one folder at a time
        } while (file_loc[counter+1] != ' ' );
        return 0;
    My Ctrl+S addiction gets in the way when using Code Blocks...

  2. #2
    Registered User
    Join Date
    Jun 2005
    Your problem is that your code will keep looping past the zero terminator at the end of the string (the inner loop looks for a '/' and the outer loop looks for a ' ').

    The net effect is that, eventually, your code is reading data past the end of file_loc (undefined behaviour) and writing it past the end of tempfile_loc (also undefined behaviour).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    So your main error is comparing to space (for some reason) instead of '\0'.

    counter is a bad name, mostly because it isn't a counter, it's an index. I'd suggest i.

    This is a little strange
    char tempfile_loc[140] = {' '};
    It's usually written like this
    char tempfile_loc[140] = " ";
    Although I don't see the purpose of it. Neither of the above is going to init the array to spaces, for instance, which seems to be what you've assumed.

    Also, it's usual to employ the string handling functions from the C library (if not the C++ string class). You may find strchr() useful.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a folder
    By taurus in forum C Programming
    Replies: 4
    Last Post: 10-04-2009, 08:17 AM
  2. Creating a Folder
    By Junior89 in forum C++ Programming
    Replies: 9
    Last Post: 11-07-2004, 12:45 AM
  3. Creating a Folder
    By Wanted420 in forum C++ Programming
    Replies: 2
    Last Post: 06-19-2003, 10:44 AM
  4. Creating a folder
    By bc17 in forum C++ Programming
    Replies: 7
    Last Post: 11-24-2002, 08:50 PM
  5. Help Creating a Folder
    By Apotheosis in forum C++ Programming
    Replies: 5
    Last Post: 09-03-2002, 10:05 AM