排除 _mkdir 失败
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
双斜杠是正确的。检查错误号以找出答案为什么失败:
注意:
可能的原因:
a
和b
(例如使用SHCreateDirectoryEx()
代替)The double slashes are correct. Check the error number to find out why it is failing:
Note that:
Likely causes:
a
andb
with the same call (use e.g.SHCreateDirectoryEx()
instead)您对您的案例中“失败”的含义相当模糊。这是否与您尝试一次创建多个文件夹有关?
仅当
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
ifd:\a
already exists. Otherwise, you'll need to first created:\a
and then created:\a\b
.Here's the code I wrote to correctly create a directory to any depth.
请注意,_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
我也收到这个错误。在我的例子中,
_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 returning13 : EACCESS
. I can't say for sure why, but changing delimiter from '\\' to '/' actually solved my problem. According to this thread, behaviour ofmkdir()
is governed by the platform, and in my case I'm usingVC++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 createC:\1\2
I first attempt to createC:
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 getEACCESS
.