16

I need to use the mkdir c++ function in VS 2008 which takes two arguments and is deprecated from VS 2005.

However this function is used in our code and I need to write a standalone product (containing only mkdir function) to debug something.

What header files do I need to import? I used direct.h, however compiler complains that the argument does not take 2 arguments (reason for this is the function was deprecated in VS 2005).

mkdir("C:\hello",0);
1

4 Answers 4

21

If you want to write cross-platform code, you can use boost::filesystem routines

#include <boost/filesystem.hpp>
boost::filesystem::create_directory("dirname");

This does add a library dependency but chances are you are going to use other filesystem routines as well and boost::filesystem has some great interfaces for that.

If you only need to make a new directory and if you are only going to use VS 2008, you can use _mkdir() as others have noted.

7
  • 5
    ISO C++ is not cross-platform? Why add the boost dependency here? I'm not going to -1 or anything, but this is overkill. Why add a link time lib dependency just for adding a directory? Boost filesystem is not header file only, you know. May 1, 2012 at 18:48
  • I've avoided filesystem-related functions because they were mostly system/compiler specific. I am not sure about mkdir()but could you point me to a reference where this is defined as standard ISO C++?
    – user267885
    May 1, 2012 at 18:52
  • 2
    @MichaelGoldshteyn: Since when is _mkdir ISO C++? Can't find it in the standard. So you either have a dependency on the compiler or on a boost library, from which the later seems much preferable.
    – Grizzly
    May 1, 2012 at 18:55
  • 2
    I believe the preceding underscore is what MS states makes it "ISO conforming", rather than meaning _mkdir itself is a member of the ISO C++ Standard.
    – user7116
    May 1, 2012 at 18:58
  • 4
    I really don't see why one should pull in Boost when a simple #if _MSC_VER plus #define mkdir(x) _mkdir(x) will suffice. Apr 22, 2013 at 19:47
11

It's deprecated, but the ISO C++ conformant _mkdir() replaced it, so use that version. All you need to call it is the directory name, its sole argument:

#include <direct.h>

void foo()
{
  _mkdir("C:\\hello"); // Notice the double backslash, since backslashes 
                       // need to be escaped
}

Here is the prototype from MSDN:

int _mkdir( const char *dirname );

0
9

My cross-platform solution (recursive):

#include <sstream>
#include <sys/stat.h>

// for windows mkdir
#ifdef _WIN32
#include <direct.h>
#endif

namespace utils
{
    /**
     * Checks if a folder exists
     * @param foldername path to the folder to check.
     * @return true if the folder exists, false otherwise.
     */
    bool folder_exists(std::string foldername)
    {
        struct stat st;
        stat(foldername.c_str(), &st);
        return st.st_mode & S_IFDIR;
    }

    /**
     * Portable wrapper for mkdir. Internally used by mkdir()
     * @param[in] path the full path of the directory to create.
     * @return zero on success, otherwise -1.
     */
    int _mkdir(const char *path)
    {
    #ifdef _WIN32
        return ::_mkdir(path);
    #else
    #if _POSIX_C_SOURCE
        return ::mkdir(path);
    #else
        return ::mkdir(path, 0755); // not sure if this works on mac
    #endif
    #endif
    }

    /**
     * Recursive, portable wrapper for mkdir.
     * @param[in] path the full path of the directory to create.
     * @return zero on success, otherwise -1.
     */
    int mkdir(const char *path)
    {
        std::string current_level = "";
        std::string level;
        std::stringstream ss(path);

        // split path using slash as a separator
        while (std::getline(ss, level, '/'))
        {
            current_level += level; // append folder to the current level

            // create current level
            if (!folder_exists(current_level) && _mkdir(current_level.c_str()) != 0)
                return -1;

            current_level += "/"; // don't forget to append a slash
        }

        return 0;
    }
}
1
  • There is a mistake in the above code in the folder_exists function. You should check the return code for error when you call the stat function. If it return -1, there is an error. On Visual Studio 2010 (at least), the function will return -1 if the folder doesn't exist and all the flag will be set to 1. I suggest you this edit: int ret = stat(dirPath.c_str(), &st); return (ret == 0) && (st.st_mode & S_IFDIR) ? true: false; This works correctly. Jan 23, 2014 at 20:26
7

Nowadays there is the _mkdir() function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.