std::experimental::filesystem::create_hard_link
来自 cppreference.cn
< cpp | experimental | fs
定义于头文件 <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 | - | 非抛出重载中用于错误报告的出参 |
[编辑] 返回值
(无)
[编辑] 异常
不接受 error_code& 参数的重载在底层操作系统 API 错误时抛出 filesystem_error,该错误使用 target 作为第一个参数,link 作为第二个参数,以及操作系统错误码作为错误码参数构造。std::bad_alloc 可能在内存分配失败时抛出。接受 error_code& 参数的重载在操作系统 API 调用失败时将其设置为操作系统 API 错误码,并在没有错误发生时执行 ec.clear()。此重载具有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'); // create regular file fs::create_hard_link("sandbox/a", "sandbox/b"); fs::remove("sandbox/a"); // read from the original file via surviving hard link char c = std::ifstream("sandbox/b").get(); std::cout << c << '\n'; fs::remove_all("sandbox"); }
输出
a
[编辑] 另请参阅
创建符号链接 (函数) | |
返回引用特定文件的硬链接数量 (函数) |