Namespaces
Variants

std::experimental::filesystem:: create_hard_link

From cppreference.net
ヘッダーで定義 <experimental/filesystem>
void create_hard_link ( const path & target, const path & link ) ;
void create_hard_link ( const path & target, const path & link, error_code & ec ) ;
(filesystem TS)

ハードリンク link を作成し、そのターゲットを target に設定します。これはPOSIXの link() 関数と同様の動作です:パス名 target は存在している必要があります。

作成されると、 link target は同じファイルを参照する2つの論理名となります(これらは equivalent です)。元の名前 target が削除された場合でも、ファイルは存在し続け、 link としてアクセス可能です。

目次

パラメータ

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ファイルシステムがその例です。

一部のファイルシステムでは、ファイルごとのリンク数に制限があります。

ディレクトリへのハードリンクは通常、スーパーユーザーに制限されています。

ハードリンクは通常、ファイルシステムの境界を越えることができません。

特別なパス名であるドット ( "." ) は、その親ディレクトリへのハードリンクです。特別なパス名であるドットドット ".." は、その親の親であるディレクトリへのハードリンクです。

#include <experimental/filesystem>
#include <fstream>
#include <iostream>
namespace fs = std::experimental::filesystem;
int main()
{
    fs::create_directories("sandbox/subdir");
    std::ofstream("sandbox/a").put('a'); // 通常ファイルを作成
    fs::create_hard_link("sandbox/a", "sandbox/b");
    fs::remove("sandbox/a");
    // 残存するハードリンク経由で元のファイルから読み取り
    char c = std::ifstream("sandbox/b").get();
    std::cout << c << '\n';
    fs::remove_all("sandbox");
}

出力:

a

関連項目

シンボリックリンクを作成する
(関数)
特定のファイルを参照するハードリンクの数を返す
(関数)