c++cvisual-c++mkdirdirent.h

Can't find mkdir() function in dirent.h for windows


I am using dirent.h 1.20 (source) for windows in VC2013.

I can't find mkdir() in it.

How am I supposed to use it? Or can I create a directory somehow only using dirent.h?


Solution

  • Update: Since C++17, <filesystem> is the portable way to go. For earlier compilers, check out Boost.Filesystem.


    The header you are linking to is effectively turning your (POSIX) dirent.h calls into (native) Windows calls. But dirent.h is about directory entries, i.e. reading directories, not creating ones.

    If you want to create a directory (mkdir()), you need either:


    // UGLY - these two don't belong in the same source...
    #include <dirent.h>
    #include <windows.h>
    
    // ...
    CreateDirectory( "D:\\TestDir", NULL );
    // ...
    

    Another solution would be to take a look at Cygwin, which provides a POSIX environment running on Windows, including Bash shell, GCC compiler toolchain, and a complete collection of POSIX headers like dirent.h, sys/stat.h, sys/types.h etc., allowing you to use the POSIX API consistently in your programming.