检测路径是绝对路径还是相对路径 使用C++,我需要检测给定路径(文件名)是绝对的还是相对的。我可以使用Windows API,但不想使用像Boost这样的第三方库,因为我需要在小型Windows应用程序中使用此解决方案,而不需要额外的依赖项。

检测路径是绝对路径还是相对路径 使用C++,我需要检测给定路径(文件名)是绝对的还是相对的。我可以使用Windows API,但不想使用像Boost这样的第三方库,因为我需要在小型Windows应用程序中使用此解决方案,而不需要额外的依赖项。,c++,windows,visual-c++,path,C++,Windows,Visual C++,Path,Windows API有。其定义如下: BOOL PathIsRelative( _In_ LPCTSTR lpszPath ); 从C++14/C++17开始,您可以使用 @LightnessRacesinOrbit:尽管它在99%的情况下都能工作,但它并不是一个完美的解决方案。原因有两个:1。从技术上讲,应该有三个返回选项:yes,no,以及错误确定。2.此限制:最大长度最大路径。不幸的是,我没有找到一个Windows API可以可靠地做到这一点……使用#include@AlexFa

Windows API有。其定义如下:

BOOL PathIsRelative(
  _In_  LPCTSTR lpszPath
);

从C++14/C++17开始,您可以使用


@LightnessRacesinOrbit:尽管它在99%的情况下都能工作,但它并不是一个完美的解决方案。原因有两个:1。从技术上讲,应该有三个返回选项:
yes
no
,以及
错误确定
。2.此限制:
最大长度最大路径
。不幸的是,我没有找到一个Windows API可以可靠地做到这一点……使用#include@AlexFarber:他的观点是,如果你尝试谷歌搜索,你就会找到正确的位置。
#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)
}
#include <experimental/filesystem> // C++14

std::experimental::filesystem::path path(winPathString); // Construct the path from a string.