std::filesystem::create_hard_link
来自 cppreference.cn
< cpp | filesystem
定义于头文件 <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 就是指向同一文件的两个逻辑名称(它们是等价的)。即使原始名称 target 被删除,文件仍然存在,并且可以通过 link 访问。
内容 |
[编辑] 参数
target | - | 要链接的文件或目录的路径 |
link | - | 新硬链接的路径 |
ec | - | 非抛出重载中用于错误报告的输出参数 |
[编辑] 返回值
(无)
[编辑] 异常
任何未标记为 noexcept
的重载都可能在内存分配失败时抛出 std::bad_alloc 异常。
1) 在底层操作系统 API 错误时抛出 std::filesystem::filesystem_error 异常,使用 target 作为第一个路径参数,link 作为第二个路径参数,以及操作系统错误代码作为错误代码参数构造。
[编辑] 注释
某些操作系统根本不支持硬链接,或者仅对常规文件支持硬链接。
某些文件系统不支持硬链接,无论操作系统如何:例如,存储卡和闪存驱动器上使用的 FAT 文件系统。
某些文件系统限制每个文件的链接数。
链接到目录通常仅限于超级用户。
硬链接通常不能跨越文件系统边界。
特殊路径名点(".")是指向其父目录的硬链接。特殊路径名点点("..")是指向其父目录的父目录的硬链接。
[编辑] 示例
运行此代码
#include <filesystem> #include <fstream> #include <iostream> namespace fs = std::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
[编辑] 参见
(C++17)(C++17) |
创建一个符号链接 (函数) |
(C++17) |
返回指向特定文件的硬链接数 (函数) |