排除 _mkdir 失败

发布于 2024-11-17 19:01:05 字数 743 浏览 4 评论 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 << "Path was not found.";
    }
}

运行程序时,输出是意外的。(大多数时候都是失败。我不知道原因)

有时我会看到成功。很多时候,如果我删除双斜杠 \\ 和单斜杠 \ 消息表示成功。 这件事让我很愤怒。为什么会出现这种情况? 是反斜杠有问题吗?

在 Visual C++ 2010 Express 版本中,当我按 CTRL+F5 时, 更新

输出仅是按任意键继续...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

还给你自由 2024-11-24 19:01:05

双斜杠是正确的。检查错误号以找出答案为什么失败:

如果创建了新目录,则每个函数都返回值 0。出现错误时,函数返回 –1 并设置 errno,如下所示:
EEXIST - 未创建目录,因为 dirname 是现有文件、目录或设备的名称。
ENOENT - 未找到路径。

注意:

_mkdir 每次调用只能创建一个新目录,因此只有 dirname 的最后一个部分可以命名新目录。

可能的原因:

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
小瓶盖 2024-11-24 19:01:05

您对您的案例中“失败”的含义相当模糊。这是否与您尝试一次创建多个文件夹有关?

仅当 d:\a 已存在时,您才能创建文件夹 d:\a\b。否则,您需要先创建 d:\a,然后创建 d:\a\b

这是我编写的代码,用于正确创建任意深度的目录。

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.

遥远的她 2024-11-24 19:01:05

请注意,_mkdir 不会返回 ENOENT 或 EEXIST,这些是调用 _mkdir 后的 errno 值。如果 _mkdir 失败,根据文档,它将始终返回 -1。

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

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

甜妞爱困 2024-11-24 19:01:05

我也收到这个错误。在我的例子中,_mkdir()实际上返回了13:EACCESS。我不能确定为什么,但将分隔符从“\\”更改为“/”实际上解决了我的问题。根据 此帖子mkdir 的行为() 由平台管理,在我的例子中,我在 Windows 7 上使用 VC++2012

更新:问题不是分隔符。为了创建一个文件夹,我在检查 _mkdir() 的结果时递归地尝试创建其父文件夹。要创建C:\1\2,我首先尝试创建C:,它不仅已经存在,而且未授予创建它的权限。似乎在实际检查文件夹是否存在之前先检查创建文件夹的权限!这就是为什么我得到EACCESS

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文