最可靠标准是用std::filesystem::path解析后调用is_absolute()或is_relative()判断格式合法性,再用exists()或status()验证实际存在性,而非正则匹配。

Windows 下用 std::filesystem::path 判断路径合法性最可靠
标准库的 std::filesystem::path 不验证路径是否存在,但能解析结构、识别根、区分相对/绝对——这是最接近“格式合法”的定义。直接调用 is_absolute() 或 is_relative() 即可,无需手动解析斜杠或盘符。
-
std::filesystem::path("C:\foo\bar")→is_absolute()返回true -
std::filesystem::path("../data/file.txt")→is_relative()返回true -
std::filesystem::path("C:foo\bar")(无反斜杠)→is_absolute()为false,因为 Windows 视其为相对路径(当前驱动器下的相对路径) - 空字符串、仅空格、含