Take the 2-minute tour ×
#include <iostream>
#include <direct.h>
using namespace std;
int main() {
 if( _mkdir("d:\\a\\b") == 0 ){
   cout << "success";
 }    else if (  _mkdir("d:\\a") == EEXIST ) {
   cout << "Directory was not created because dirname is the name of an existing   file, directory, or device.";
         else if (  _mkdir("d:\\a") == ENOENT ) {
      cout << "Path was not found.";

On running the program the output is unexpected.(Most of the times it is a fail. I don't know the reason)

sometimes i see success.Many times i if i remove double slash \\ with single slash \ the message is success . This thing is making me furious. Why is this happening ? Are backslashes the problem ?


in visual c++ 2010 express edition when i press CTRL+F5 the output is only press any key to continue...

share|improve this question
When _mkdir fails try to look at the value of global variable errno, it will probably give you the reason. –  Serge Jun 30 '11 at 13:14
No, you can't just test the return value against EEXIST and ENOENT, you have to check errno. –  Ben Voigt Jun 30 '11 at 13:32
..and that was b'coz i was trying to create both a and b with the same call –  Suhail Gupta Jun 30 '11 at 13:34
add comment

4 Answers

up vote 6 down vote accepted

The double slashes are correct. Check the error number to find out why it is failing:

Each of these functions returns the value 0 if the new directory was created. On an error the function returns –1 and sets errno as follows:
EEXIST - Directory was not created because dirname is the name of an existing file, directory, or device. ENOENT - Path was not found.

Note that:


Likely causes:

  • you are trying to create both a and b with the same call (use e.g. SHCreateDirectoryEx() instead)
  • the directory already exists
share|improve this answer
_mkdir, _wmkdir –  crashmstr Jun 30 '11 at 13:11
add comment

You've been rather vague about what fails means in your case. Could it be related to the fact that you are trying to create multiple folders at once?

You can only create the folder d:\a\b if d:\a already exists. Otherwise, you'll need to first create d:\a and then create d:\a\b.

Here's the code I wrote to correctly create a directory to any depth.

share|improve this answer
add comment

Note that _mkdir doesn't return ENOENT or EEXIST, those are the values of errno after the call to _mkdir. If _mkdir fails, it will always return -1 according to the documentation.


share|improve this answer
add comment

I received this error too. In my case _mkdir() was actually returning 13 : EACCESS. I can't say for sure why, but changing delimiter from '\\' to '/' actually solved my problem. According to this thread, behaviour of mkdir() is governed by the platform, and in my case I'm using VC++2012 on Windows 7.

Update: Problem is not the delimiter. To create a folder, I recursively attempt to create its parents while checking for _mkdir()'s result. To create C:\1\2 I first attempt to create C: which not only already exists, but also the permission to create it, is not granted. Seems that permission to create a folder is checked before the actual check if its existing! That's why I get EACCESS.

share|improve this answer
add comment

Your Answer


