Namespaces
Variants

std::experimental::filesystem:: copy_file

From cppreference.net
定義済みヘッダー <experimental/filesystem>
bool copy_file ( const path & from, const path & to ) ;
bool copy_file ( const path & from, const path & to, error_code & ec ) ;
(1) (filesystem TS)
bool copy_file ( const path & from, const path & to, copy_options options ) ;
bool copy_file ( const path & from, const path & to, copy_options options, error_code & ec ) ;
(2) (filesystem TS)
1) デフォルトは、 (2) において copy_options::none options として使用された場合と同等です。
2) from から to へ単一ファイルをコピーし、 options で指定されたコピーオプションを使用します。 options 内のいずれかの copy_options オプショングループに複数のオプションが存在する場合( copy_file に関連しないグループであっても)、動作は未定義です。
  • 宛先ファイルが存在しない場合、
  • from が解決するファイルの内容と属性を、 to が解決するファイルへコピーします(シンボリックリンクは追従されます)。
  • それ以外の場合、宛先ファイルが既に存在する場合:
  • to from equivalent(from, to) によって同一と判定された場合、エラーを報告する。
  • それ以外の場合、 options にcopy_file制御オプションが一つも設定されていない場合、エラーを報告する。
  • それ以外の場合、 options copy_options::skip_existing が設定されている場合、何も行わない。
  • それ以外の場合、 options copy_options::overwrite_existing が設定されている場合、 from が解決するファイルの内容と属性を to が解決するファイルにコピーする。
  • それ以外の場合、 options copy_options::update_existing が設定されている場合、 last_write_time() で定義される from to より新しい場合にのみファイルをコピーする。

例外を送出しないオーバーロードは、 エラーが発生した場合に false を返します。

目次

翻訳の説明: - 「Contents」を「目次」に翻訳しました - C++関連の専門用語(Parameters、Return value、Exceptions、Notes、Example、See also)は原文のまま保持しました - HTMLタグ、属性、クラス名、ID、リンク先は一切変更していません - 数値や書式設定は完全に保持されています

パラメータ

from - ソースファイルへのパス
to - ターゲットファイルへのパス
ec - 非スローオーバーロードでのエラー報告用出力パラメータ

戻り値

true ファイルがコピーされた場合、 false それ以外の場合。

例外

The overload that does not take an error_code & parameter throws filesystem_error on underlying OS API errors, constructed with from as the first argument, to 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

注記

これらの関数は、最大でも1回の直接または間接的な status(to) 呼び出しを含みます(ファイルの存在確認と、 copy_options::update_existing オプションの場合には最終更新時刻の確認の両方に使用されます)。

ディレクトリのコピーに copy_file を使用するとエラーが報告されます:その場合は copy を使用してください。

copy_file はシンボリックリンクを追従します:その場合は copy_symlink または copy copy_options::copy_symlinks オプションで使用してください。

#include <experimental/filesystem>
#include <fstream>
#include <iostream>
namespace fs = std::experimental::filesystem;
int main()
{
    fs::create_directory("sandbox");
    std::ofstream("sandbox/file1.txt").put('a');
    fs::copy_file("sandbox/file1.txt", "sandbox/file2.txt");
    // 現在、sandboxには2つのファイルが存在します:
    std::cout << "file1.txt holds : "
              << std::ifstream("sandbox/file1.txt").rdbuf() << '\n';
    std::cout << "file2.txt holds : "
              << std::ifstream("sandbox/file2.txt").rdbuf() << '\n';
    // ディレクトリのコピーに失敗
    fs::create_directory("sandbox/abc");
    try
    {
        fs::copy_file("sandbox/abc", "sandbox/def");
    }
    catch (fs::filesystem_error& e)
    {
        std::cout << "Could not copy sandbox/abc: " << e.what() << '\n';
    }
    fs::remove_all("sandbox");
}

出力例:

file1.txt holds : a
file2.txt holds : a
Could not copy sandbox/abc: copy_file: Is a directory: "sandbox/abc", "sandbox/def"

関連項目

コピー操作のセマンティクスを指定する
(列挙型)
シンボリックリンクをコピーする
(関数)
ファイルまたはディレクトリをコピーする
(関数)