Namespaces
Variants

std::experimental::filesystem:: equivalent

From cppreference.net
定义于头文件 <experimental/filesystem>
bool equivalent ( const path & p1, const path & p2 ) ;
bool equivalent ( const path & p1, const path & p2, error_code & ec ) ;
(1) (文件系统 TS)

检查路径 p1 p2 是否指向同一文件或目录,并具有通过 status 确定的相同文件状态(符号链接会被跟踪)。

如果 p1 p2 不存在,或者它们的文件类型不是普通文件、目录或符号链接(由 is_other 判定),将报告错误。

非抛出重载在出错时返回 false

目录

参数

p1, p2 - 用于检查等效性的路径
ec - 非抛出重载中用于错误报告的输出参数

返回值

p1 p2 指向相同文件或目录且其文件状态一致时返回 true ,否则返回 false

异常

The overload that does not take an error_code & parameter throws filesystem_error on underlying OS API errors, constructed with p1 as the first argument, p2 as the second argument, and the OS error code as the error code argument. std:: bad_alloc may be thrown if memory allocation fails. The overload taking an error_code & parameter sets it to the OS API error code if an OS API call fails, and executes ec. clear ( ) if no errors occur. This overload has
noexcept 规范:
noexcept

注释

如果两个路径的POSIX stat结构体 (通过POSIX stat 函数获取)中的 st_dev st_ino 字段相等(即文件位于同一设备的相同存储位置),则认为这两个路径解析为相同的文件系统实体。

特别地,同一文件或目录的所有硬链接都是等效的,而同一文件系统上的符号链接与其目标也是等效的。

示例

#include <cstdint>
#include <experimental/filesystem>
#include <iostream>
namespace fs = std::experimental::filesystem;
int main()
{
    // 硬链接等效性检查
    fs::path p1 = ".";
    fs::path p2 = fs::current_path();
    if (fs::equivalent(p1, p2))
        std::cout << p1 << " is equivalent to " << p2 << '\n';
    // 符号链接等效性检查
    fs::path p3 = "/lib/libc.so.6";
    fs::path p4 = p3.parent_path() / fs::read_symlink(p3);
    if (fs::equivalent(p3, p4))
        std::cout << p3 << " is equivalent to " << p4 << '\n';
}

可能的输出:

"." is equivalent to "/var/tmp/test"
"/lib/libc.so.6" is equivalent to "/lib/libc-2.12.so"

参见

确定文件属性
确定文件属性,检查符号链接目标
(函数)