我偶然发现了GetFullPathName()(通过使用QFileInfo::canonicalFilePath())我不太明白的行为:当我用一个由当前驱动器号和冒号组成的字符串调用此函数时,它返回当前工作目录的路径,而我期待通往驱动器号的路径.
以下代码举例说明了我所说的内容:
#include <windows.h>
#include <iostream>
#include <string>
std::string canonicalFilePath(const char *path)
{
static const std::size_t BufferSize = 300;
char canonicalPath[BufferSize];
GetFullPathName(path, BufferSize, canonicalPath, 0);
return std::string(canonicalPath);
}
int main(int, char **)
{
SetCurrentDirectory("C:/some/path");
std::cout << "In C:" << '\n';
std::cout << " C -> " << canonicalFilePath("C") << '\n'
<< " C: -> " << canonicalFilePath("C:") << '\n'
<< " C:/ -> " << canonicalFilePath("C:/") << '\n'
<< " D -> " << canonicalFilePath("D") << '\n'
<< " D: -> " << canonicalFilePath("D:") << '\n'
<< " D:/ -> " << canonicalFilePath("D:/") << '\n';
SetCurrentDirectory("D:/other/path");
std::cout << "In D:" << '\n';
std::cout << " C -> " << canonicalFilePath("C") << '\n'
<< " C: -> " << canonicalFilePath("C:") << '\n'
<< " C:/ -> " << canonicalFilePath("C:/") << '\n'
<< " D -> " << canonicalFilePath("D") << '\n'
<< " D: -> " << canonicalFilePath("D:") << '\n'
<< " D:/ -> " << canonicalFilePath("D:/") << '\n';
}
Run Code Online (Sandbox Code Playgroud)
输出:
In C:
C -> C:\some\path\C // ok
C: -> C:\some\path // ? why not C:\ ?
C:/ -> C:\ // ok
D -> C:\some\path\D // ok
D: -> D:\ // ok
D:/ -> D:\ // ok
In D:
C -> D:\other\path\C // ok
C: -> C:\ // ok
C:/ -> C:\ // ok
D -> D:\other\path\D // ok
D: -> D:\other\path // ? why not D:\ ?
D:/ -> D:\ // ok
Run Code Online (Sandbox Code Playgroud)
这种行为是否正常?在GetFullPathName文档中,有人说
如果指定"U:",则返回的路径为"U:\"
如果"U"是当前的驱动器号,为什么不是这种情况?
| 归档时间: |
|
| 查看次数: |
3209 次 |
| 最近记录: |