Ale*_*x F 14 c++ windows path visual-c++
使用C++,我需要检测给定的路径(文件名)是绝对的还是相对的.我可以使用Windows API,但不想使用像Boost这样的第三方库,因为我需要在没有经验依赖性的小型Windows应用程序中使用此解决方案.
Dan*_*ite 20
Windows API有PathIsRelative.它被定义为:
BOOL PathIsRelative(
_In_ LPCTSTR lpszPath
);
Run Code Online (Sandbox Code Playgroud)
从 C++14/C++17 开始,您可以使用is_absolute()和is_relative()来自文件系统库
#include <filesystem> // C++17 (or Microsoft-specific implementation in C++14)
std::string winPathString = "C:/tmp";
std::filesystem::path path(winPathString); // Construct the path from a string.
if (path.is_absolute()) {
// Arriving here if winPathString = "C:/tmp".
}
if (path.is_relative()) {
// Arriving here if winPathString = "".
// Arriving here if winPathString = "tmp".
// Arriving here in windows if winPathString = "/tmp". (see quote below)
}
Run Code Online (Sandbox Code Playgroud)
路径“/”在 POSIX 操作系统上是绝对的,但在 Windows 上是相对的。
在 C++14 中使用 std::experimental::filesystem
#include <experimental/filesystem> // C++14
std::experimental::filesystem::path path(winPathString); // Construct the path from a string.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10119 次 |
| 最近记录: |