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);
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Bruce
  • 2,133
  • 12
  • 34
  • 52
  • For a modern approach in C++17, see this answer: https://stackoverflow.com/a/56943302/6674213 – brad Dec 28 '20 at 16:48

4 Answers4

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.

  • 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. – Michael Goldshteyn May 01 '12 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++? –  May 01 '12 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 01 '12 at 18:55
  • @Grizzly, MSDN documents it as such: http://msdn.microsoft.com/en-us/library/ms235326(v=vs.80).aspx . I will try to find a better reference though, stay tuned. OK, my bad, it has a leading underscore for ISO compliance, but it is not an ISO C++ standard function. – Michael Goldshteyn May 01 '12 at 18:56
  • 2
    I believe the preceding underscore is what MS states makes it ["ISO conforming"](http://stackoverflow.com/questions/814975/getch-is-deprecated), rather than meaning `_mkdir` itself is a member of the ISO C++ Standard. – user7116 May 01 '12 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. – Ivan Vučica Apr 22 '13 at 19:47
  • A solution that works in Linux and don't need boost: https://stackoverflow.com/questions/7430248/creating-a-new-directory-in-c. – keineahnung2345 Nov 07 '19 at 10:05
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 );

Michael Goldshteyn
  • 71,784
  • 24
  • 131
  • 181
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;
    }
}
Francesco Noferi
  • 482
  • 7
  • 13
  • 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. – Jean-Philippe Jodoin Jan 23 '14 at 20:26
7

Nowadays there is the _mkdir() function.

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
Electro
  • 2,994
  • 5
  • 26
  • 32