GetFileAttributesA 正在返回现有目录的"17"。 "16"意味着它是一个目录,文档中没有提到"17"

GetFileAttributesA is returning "17" for an existing directory. "16" means it's a directory and there's no mention of "17" in documentation

本文关键字:一个 文档 意味着 返回 GetFileAttributesA      更新时间:2023-10-16

Windows 7 64位,使用mingw编译。我正在尝试测试给定路径是否是使用Windows标题中的GetFileatTributesa的目录。目录的常数为16。由于某种原因,它返回17。我的代码看起来像这样:

#include <iostream>
#include <windows.h>
void dir_exists(std::string dir_path)
{
    DWORD f_attrib = GetFileAttributesA(dir_path.c_str());
    std::cout << "Current: " << f_attrib << std::endl <<
        "Wanted: " <<
        FILE_ATTRIBUTE_DIRECTORY << std::endl;
}
int main()
{
    dir_exists("C:\Users\");
    return 0;
}

当我运行此功能时,输出为:

Current: 17  
Wanted: 16

电流应在此处返回16。正如我在主题中所说的那样,我什至找不到任何17在文档中的含义。

GetFileAttributes返回一个bitmask,此处列出的有效值:文件属性常数。

17 == 0x11,所以这意味着返回值是
FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_DIRECTORY

如果您只想检测您的路径是否指向目录,请用FILE_ATTRIBUTE_DIRECTORY掩盖返回值,然后查看是否非零:

#include <string>
#include <iostream>
#include <windows.h>
bool dir_exists(std::string const& dir_path)
{
    DWORD const f_attrib = GetFileAttributesA(dir_path.c_str());
    return f_attrib != INVALID_FILE_ATTRIBUTES &&
           (f_attrib & FILE_ATTRIBUTE_DIRECTORY);
}
int main()
{
    std::cout << dir_exists("C:\Users\") << 'n';
}