std::filesystem:: create_hard_link
|
定义于头文件
<filesystem>
|
||
|
void
create_hard_link
(
const
std::
filesystem
::
path
&
target,
const std:: filesystem :: path & link ) ; |
(1) | (C++17 起) |
|
void
create_hard_link
(
const
std::
filesystem
::
path
&
target,
const
std::
filesystem
::
path
&
link,
|
(2) | (C++17 起) |
创建一个硬链接
link
,其目标设置为
target
,操作方式遵循 POSIX
link()
规范:路径名
target
必须已存在。
一旦创建, link 和 target 就成为指向同一文件的两个逻辑名称(它们是 equivalent 的)。即使原始名称 target 被删除,该文件仍然存在,并可通过 link 进行访问。
目录 |
参数
| target | - | 要链接到的文件或目录路径 |
| link | - | 新硬链接的路径 |
| ec | - | 非抛出重载中用于错误报告的输出参数 |
返回值
(无)
异常
任何未标记为
noexcept
的重载在内存分配失败时可能抛出
std::bad_alloc
。
注释
某些操作系统完全不支持硬链接,或仅对常规文件支持硬链接。
某些文件系统无论操作系统如何都不支持硬链接:例如在存储卡和闪存驱动器上使用的FAT文件系统。
某些文件系统限制每个文件的链接数量。
对目录的硬链接通常仅限于超级用户。
硬链接通常不能跨越文件系统边界。
特殊路径名点号( "." )是指向其父目录的硬链接。特殊路径名双点号 ".." 是指向其父目录的父目录的硬链接。
示例
#include <filesystem> #include <fstream> #include <iostream> namespace fs = std::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
参见
|
(C++17)
(C++17)
|
创建符号链接
(函数) |
|
(C++17)
|
返回指向特定文件的硬链接数量
(函数) |