c++ - 在 Windows 上,什么时候需要附加到目录路径才能使 _stat 成功?

标签 c++ windows

我已经在 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/

相关文章:

c++ - 在 Android 上使用 Boost.Log 进行 logcat

C++ 模板代码适用于 msvc 2015 但不适用于 2017

windows - 我如何从 IMMDevice 获取信息?

python-3.x - 在 Python 3.6 Windows 上安装 PyMesh

c++ - C++ 中的 3-d 动画

符合 c++ STL 的迭代器迭代器

c++ - C++修改Lua中 "arg"的方法

c++ - 如何获取桌面的窗口句柄?

c# - 如何从 DataGridViewComboBoxColumn 获取选定值?

linux - 使用 perl 从 linux 服务器启动批处理文件