Search code examples
c++visual-c++visual-c++-2010-express

troubleshooting _mkdir failure


#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 ?

UPDATE

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


Solution

  • 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:

    _mkdir can create only one new directory per call, so only the last component of dirname can name a new directory.

    Likely causes:

    • you are trying to create both a and b with the same call (use e.g. SHCreateDirectoryEx() instead)
    • the directory already exists