我已经在 VS2010 和 VS2002 下测试了 _stat: 目录 c:\system\已经存在于我的 PC 中。
struct _stat filestat;
unsigned int n1 = _stat("c:\\system",&filestat);
unsigned int n2 = _stat("c:\\",&filestat);
unsigned int n3 = _stat("c:\\system\\",&filestat);
unsigned int n4 = _stat("c:",&filestat);
结果:
n1=0;
n2=0;
n3=0xffffffff;
n4=0xffffffff;
谁能知道我为什么以及什么时候需要在目录路径中放置“\”?
最佳答案
在 Windows 上,我认为 none 那些 stat
调用应该失败(假设,如您所说,c:\system
目录确实存在)。
请运行以下测试程序并将其输出编辑到您的问题中:
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
static void test_stat(const char *path)
{
struct _stat st;
if (_stat(path, &st))
printf("%s: %s\n", path, strerror(errno));
else
printf("%s: success, mode=%x\n", path, st.st_mode);
}
int main(void)
{
test_stat("c:");
test_stat("c:\\");
test_stat("c:\\system");
test_stat("c:\\system\\");
return 0;
}
(如果您没有意识到可以编辑您的问题,请立即在蓝色“c++”标签下方查看一行灰色的小字。这些是按钮。其中一个应该是“编辑”。)
关于c++ - 在 Windows 上,什么时候需要附加到目录路径才能使 _stat 成功?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42545456/