命名空间
变体
操作

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

来自 cppreference.cn
< cpp‎ | experimental‎ | fs
 
 
实验性
技术规范
文件系统库 (filesystem TS)
库基础 (library fundamentals TS)
库基础 2 (library fundamentals TS v2)
库基础 3 (library fundamentals TS v3)
并行扩展 (parallelism TS)
并行扩展 2 (parallelism TS v2)
并发扩展 (concurrency TS)
并发扩展 2 (concurrency TS v2)
概念 (concepts TS)
范围 (ranges TS)
反射 (reflection TS)
数学特殊函数 (special functions TR)
实验性非TS
模式匹配
线性代数
std::execution
契约
2D图形
 
 
定义于头文件 <experimental/filesystem>
void create_symlink( const path& target, const path& link );
void create_symlink( const path& target, const path& link, error_code& ec );
(1) (filesystem 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) (filesystem TS)

创建符号链接 link,其目标设置为 target,如同 POSIX symlink() 所为:路径名 target 可以是无效的或不存在的。

某些操作系统需要符号链接创建来标识链接是否指向目录。可移植代码应使用 (2) 创建目录符号链接,而不是 (1),即使在 POSIX 系统上没有区别。

目录

[编辑] 参数

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

[编辑] 返回值

(无)

[编辑] 异常

不接受 error_code& 参数的重载在底层操作系统 API 错误时抛出 filesystem_error,构造时使用 target 作为第一个参数,link 作为第二个参数,以及操作系统错误代码作为错误代码参数。如果内存分配失败,可能抛出 std::bad_alloc。接受 error_code& 参数的重载在操作系统 API 调用失败时将其设置为操作系统 API 错误代码,并在没有错误发生时执行 ec.clear()。此重载具有
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"

[编辑] 参见

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