5

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?

Rakesh Malik
  • 607
  • 1
  • 6
  • 27
  • 1
    `mkdir` isn't supposed to live in ``. On POSIX systems, you get it from `` and `` (yes, you need both). – Fred Foo May 02 '14 at 12:16
  • 3
    If boost is an option for you: It has a [file system library](http://www.boost.org/doc/libs/1_55_0/libs/filesystem/doc/index.htm), which offers the same functionality in a portable way. – lethal-guitar May 02 '14 at 12:17
  • You have [_mkdir](http://msdn.microsoft.com/en-us/library/2fkk4dzw(v=vs.100).aspx) as part of the C library. So unless you need something else out of that other library, you don't need it for creating a directory (`#include `). – crashmstr May 02 '14 at 12:30
  • 1
    possible duplicate of [How to create a folder in C (need to run on both Linux and Windows)](http://stackoverflow.com/questions/23271990/how-to-create-a-folder-in-c-need-to-run-on-both-linux-and-windows) – alk May 02 '14 at 13:03

5 Answers5

13

simplest way that helped without using any other library is.

#if defined _MSC_VER
#include <direct.h>
#elif defined __GNUC__
#include <sys/types.h>
#include <sys/stat.h>
#endif

void createDir(string dir) {
#if defined _MSC_VER
    _mkdir(dir.data());
#elif defined __GNUC__
    mkdir(dir.data(), 0777);
#endif
}
Rakesh Malik
  • 607
  • 1
  • 6
  • 27
5

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:

  • A similar wrapping header turning your (POSIX) mkdir() call into the corresponding (native) Windows function calls (and I cannot point out such a header for you), or
  • use the Windows API directly, which might be pragmatic but would lead to a really ugly mix of POSIX and Windows functions...

// 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.

DevSolar
  • 67,862
  • 21
  • 134
  • 209
2

Visual Studio includes the <direct.h> header. This header declares _mkdir and _wmkdir, which can be used to create a directory, and are part of the C libraries included with Visual Studio.

The other "easy" option would be to use Windows API calls as indicated by DevSolar.

Community
  • 1
  • 1
crashmstr
  • 28,043
  • 9
  • 61
  • 79
0

You can use sys/types.h header file and use
mkdir(const char*) method to create a directory
Following is the sample code

#include<stdio.h>
#include<string.h>
#include <unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
int main()
{
    if(!mkdir("C:mydir"))
    {
        printf("File created\n");
    }
    else
        printf("Error\n");
}
  • 1
    Note that [Microsoft say the \[mkdir\] POSIX function is deprecated. Use the ISO C++ conformant _mkdir instead](https://learn.microsoft.com/en-gb/cpp/c-runtime-library/reference/mkdir): " – Anon Nov 04 '17 at 13:52
0

mkdir is deprecated. Give #include <direct.h> as a header file. then write

_mkdir("C:/folder")
shawon
  • 984
  • 7
  • 15