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 ); |
(filesystem 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
[编辑] 参见
创建一个符号链接 (function) | |
返回指向特定文件的硬链接数 (function) |