Namespaces
Variants

std::experimental::filesystem:: create_hard_link

From cppreference.net
定义于头文件 <experimental/filesystem>
void create_hard_link ( const path & target, const path & link ) ;
void create_hard_link ( const path & target, const path & link, error_code & ec ) ;
(文件系统 TS)

创建一个硬链接 link ,其目标设置为 target ,操作方式遵循 POSIX link() 规范:路径名 target 必须已存在。

一旦创建, link target 就成为指向同一文件的两个逻辑名称(它们是 equivalent 的)。即使原始名称 target 被删除,该文件仍继续存在,并可通过 link 进行访问。

目录

参数

target - 要链接到的文件或目录路径
link - 新硬链接的路径
ec - 非抛出重载中用于错误报告的输出参数

返回值

(无)

异常

The overload that does not take an error_code & parameter throws filesystem_error on underlying OS API errors, constructed with target as the first argument, link 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

注释

某些操作系统完全不支持硬链接,或仅对常规文件支持硬链接。

某些文件系统无论操作系统如何都不支持硬链接:例如,在存储卡和闪存驱动器上使用的FAT文件系统。

某些文件系统限制每个文件的链接数量。

对目录创建硬链接通常仅限于超级用户权限。

硬链接通常不能跨越文件系统边界。

特殊路径名点号( "." )是指向其父目录的硬链接。特殊路径名双点号 ".." 是指向其父目录的父目录的硬链接。

示例

#include <experimental/filesystem>
#include <fstream>
#include <iostream>
namespace fs = std::experimental::filesystem;
int main()
{
    fs::create_directories("sandbox/subdir");
    std::ofstream("sandbox/a").put('a'); // 创建常规文件
    fs::create_hard_link("sandbox/a", "sandbox/b");
    fs::remove("sandbox/a");
    // 通过存留的硬链接从原始文件读取
    char c = std::ifstream("sandbox/b").get();
    std::cout << c << '\n';
    fs::remove_all("sandbox");
}

输出:

a

参见

创建符号链接
(函数)
返回指向特定文件的硬链接数量
(函数)