首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::filesystem::create_hard_link

Defined in header <filesystem>

?

?

void create_hard_link( const std::filesystem::path& target, const path& std::filesystem::link ); void create_hard_link( const std::filesystem::path& target, const path& std::filesystem::link, std::error_code& ec );

?

(since C++17)

创建硬链接link它的目标设置为target好像是由POSIX链接%28%29*路径名称target一定存在。

一旦被创造,linktarget是两个引用相同文件%28的逻辑名称,它们是equivalent29%。即使原来的名字target,则该文件将继续存在,并且可作为link...

参数

target

-

path of the file or directory to link to

link

-

path of the new hard link

ec

-

out-parameter for error reporting in the non-throwing overload

返回值

%280%29

例外

不占用std::error_code&参数抛文件系统[医]误差关于基础OS API错误,使用target作为第一个论点,link作为第二个参数,操作系统错误代码作为错误代码参数。std::bad_alloc如果内存分配失败,则可能引发。过载std::error_code&参数,如果OSAPI调用失败,则将其设置为OSAPI错误代码,并执行ec.clear()如果没有错误发生。这个过载

noexcept规格:

noexcept

注记

有些操作系统根本不支持硬链接,或者只支持常规文件。

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

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

到目录的硬链接通常仅限于超级用户。

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

特殊路径名点%28"."%29是指向其父目录的硬链接。特殊路径名点点".."是指向其父目录的硬链接。

二次

代码语言:javascript
复制
#include <iostream>
#include <fstream>
#include <filesystem>
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");
}

二次

产出:

二次

代码语言:javascript
复制
a

二次

另见

create_symlinkcreate_directory_symlink (C++17)(C++17)

creates a symbolic link (function)

hard_link_count (C++17)

returns the number of hard links referring to the specific file (function)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com