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
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';
}
相关文章:
- 正在查找文档以获得PS4平台的C++中的设备信息
- 如何在文档文件夹中创建目录
- 当Microsoft文档仅包含 C# 示例时,如何查找 C++ 包含文件名
- 通过构造函数创建一些值并尝试添加到文档中使用 rapidjson 不起作用
- 关于类的 Python 文档 - 对C++的引用不正确
- C++/autoconf 等效于文档库要求.txt的要求是什么?
- 如何使用基于范围的for循环迭代Rapidjson文档(它本身就是一个JSON数组)
- 如何使用PHP将上传到本地主机的一个html文件重定向到磁盘上的另一个html文档
- Rapidjson将密钥值从一个文档对象分配到另一个文档对象
- 无法在硬件模式下创建 SGX 安全区 - "invalid launch token"即使文档将无效的启动令牌指定为第一个
- 使用RapidXML遍历一个简单的DOM Xml文档
- 这是文档中的一个小错误还是我错过了一些错误
- GetFileAttributesA 正在返回现有目录的"17"。 "16"意味着它是一个目录,文档中没有提到"17"
- 需要在c++中为xml文档提供一个通过xpath导航的算法
- 检查一个文档是否包含另一个c++的内容
- 有没有一个工具可以解析C++来强制文档
- 谁能从标记文档中给我一个此代码的主程序
- SFML 2.1 C++文档-实际上是sf::Event的一个联合
- 是否有一个(比较高级的)c++库来创建在iOS和Android上运行的pdf文档?
- 我可以从 MFC 中的另一个文档访问一个文档的数据吗