Namespaces
Variants

std::experimental::filesystem:: create_symlink, std::experimental::filesystem:: create_directory_symlink

From cppreference.net
定义于头文件 <experimental/filesystem>
void create_symlink ( const path & target, const path & link ) ;
void create_symlink ( const path & target, const path & link, error_code & ec ) ;
(1) (文件系统 TS)
void create_directory_symlink ( const path & target, const path & link ) ;
void create_directory_symlink ( const path & target, const path & link, error_code & ec ) ;
(2) (文件系统 TS)

创建一个符号链接 link ,其目标设置为 target ,操作方式类似于 POSIX symlink() :路径名 target 可能无效或不存在。

某些操作系统需要通过符号链接创建来标识该链接指向目录。可移植代码应使用 (2) 创建目录符号链接而非 (1) ,尽管在 POSIX 系统上二者并无区别。

目录

参数

target - 符号链接指向的目标路径,无需实际存在
link - 新符号链接的路径
ec - 非抛出重载中用于错误报告的输出参数

返回值

(无)

异常

The overload that does not take an error_code & parameter throws filesystem_error on underlying OS API errors, constructed with target as the first argument, link as the second argument, and the OS error code as the error code argument. std:: bad_alloc may be thrown if memory allocation fails. The overload taking an error_code & parameter sets it to the OS API error code if an OS API call fails, and executes ec. clear ( ) if no errors occur. This overload has
noexcept 规范:
noexcept

注释

某些操作系统完全不支持符号链接,或仅对常规文件支持符号链接。

某些文件系统不支持符号链接,无论操作系统如何,例如某些存储卡和闪存驱动器上使用的FAT系统。

与硬链接类似,符号链接允许文件拥有多个逻辑名称。硬链接的存在保证了文件的存在,即使原始名称已被删除。符号链接则不提供这种保证;实际上,在创建链接时,由 target 参数命名的文件无需存在。符号链接可以跨越文件系统边界。

示例

#include <experimental/filesystem>
#include <iostream>
namespace fs = std::experimental::filesystem;
int main()
{
    fs::create_directories("sandbox/subdir");
    fs::create_symlink("target", "sandbox/sym1");
    fs::create_directory_symlink("subdir", "sandbox/sym2");
    for (auto it = fs::directory_iterator("sandbox"); it != fs::directory_iterator(); ++it)
        if (is_symlink(it->symlink_status()))
            std::cout << *it << "->" << read_symlink(*it) << '\n';
    fs::remove_all("sandbox");
}

可能的输出:

"sandbox/sym1"->"target"
"sandbox/sym2"->"subdir"

参见

确定文件属性
确定文件属性,检查符号链接目标
(函数)
获取符号链接的目标
(函数)
创建硬链接
(函数)