开发者

troubleshooting _mkdir failure

开发者 https://www.devze.com 2023-03-16 05:51 出处:网络
#include <iostream> #include <direct.h> using namespace std; int main() { if( _mkdir(\"d:\\\\a\\\\b\") == 0 ){
#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 <&开发者_JAVA百科lt; "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...


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


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.


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.

http://msdn.microsoft.com/en-us/library/2fkk4dzw(v=vs.80).aspx


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消