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) (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 は無効または存在しない場合があります。

一部のオペレーティングシステムでは、リンクがディレクトリを指すことを識別するためにシンボリックリンクの作成が必要です。ポータブルなコードでは、POSIXシステムでは区別がないにもかかわらず、 (1) ではなく (2) を使用してディレクトリシンボリックリンクを作成するべきです。

目次

パラメータ

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"

関連項目

ファイル属性を決定する
シンボリックリンクのターゲットをチェックしてファイル属性を決定する
(function)
シンボリックリンクのターゲットを取得する
(function)
ハードリンクを作成する
(function)