最終更新日時(UTC):
が更新

履歴 編集

function
<filesystem>

std::filesystem::copy_file(C++17)

namespace std::filesystem {
  bool copy_file(const path& from, const path& to);                       // (1)
  bool copy_file(const path& from, const path& to, std::error_code& ec);  // (2)

  bool copy_file(const path& from, const path& to, copy_options options); // (3)
  bool copy_file(const path& from, const path& to, copy_options options,
                 std::error_code& ec);                                    // (4)
}

概要

ファイルをコピーする。

要件

  • optionsは、各グループのオプションが最大ひとつまで設定されていること

効果

戻り値

ファイルのコピーが行われたらtrue、そうでなければfalseが返る。

(2)と(4)でエラーが発生した場合もfalseが返る。

計算量

直接的もしくは間接的なstatus(to)の呼び出しは、最大で一回

例外

  • (1), (3) : ファイルシステムがエラーを報告する場合がある。エラーが発生した場合は、std::filesystem::filesystem_error例外を送出する
  • (2), (4) : OSがファイルコピーの直接のAPIを定義していない場合、この関数の実装として動的なバッファを確保する可能性がある。その際、メモリ確保で例外が発生する可能性がある

#include <cassert>
#include <filesystem>
#include <fstream>

namespace fs = std::filesystem;

int main()
{
  std::ofstream{"regular.txt"};

  // ファイル"regular.txt"を、"copy.txt"にコピーする
  fs::copy_file("regular.txt", "copy.txt");

  assert(fs::exists("regular.txt"));
  assert(fs::exists("copy.txt"));

  // 同じパスではなく、シンボリックリンク/ハードリンクでもないので、等価ではない
  assert(!fs::equivalent("regular.txt", "copy.txt"));
}

出力

バージョン

言語

  • C++17

処理系

参照