Namespaces
Variants

std::experimental::filesystem:: copy_file

From cppreference.net
Definiert im Header <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) Der Standardwert, äquivalent zu (2) mit copy_options::none als options .
2) Kopiert eine einzelne Datei von from nach to , unter Verwendung der durch options angegebenen Kopieroptionen. Das Verhalten ist undefiniert, wenn mehr als eine Option in einer der copy_options Optionsgruppen in options vorhanden ist (selbst in den für copy_file irrelevanten Gruppen).
  • Wenn die Zieldatei nicht existiert,
  • kopiert die Inhalte und Attribute der Datei, auf die from verweist, in die Datei, auf die to verweist (symbolischen Links wird gefolgt).
  • Andernfalls, wenn die Zieldatei bereits existiert:
  • Falls to und from identisch sind, wie durch equivalent(from, to) bestimmt, wird ein Fehler gemeldet.
  • Andernfalls, falls keine der copy_file-Steueroptionen in options gesetzt ist, wird ein Fehler gemeldet.
  • Andernfalls, falls copy_options::skip_existing in options gesetzt ist, wird keine Aktion ausgeführt.
  • Andernfalls, falls copy_options::overwrite_existing in options gesetzt ist, werden die Inhalte und Attribute der Datei, auf die from verweist, in die Datei kopiert, auf die to verweist.
  • Andernfalls, falls copy_options::update_existing in options gesetzt ist, wird die Datei nur kopiert, wenn from neuer als to ist, wie durch last_write_time() definiert.

Die nicht-werfenden Überladungen geben false zurück, falls ein Fehler auftritt.

Inhaltsverzeichnis

Parameter

from - Pfad zur Quelldatei
to - Pfad zur Zieldatei
ec - Out-Parameter für Fehlerberichterstattung in der nicht-werfenden Überladung

Rückgabewert

true wenn die Datei kopiert wurde, false andernfalls.

Ausnahmen

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, zu 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 Spezifikation:
noexcept

Hinweise

Die Funktionen beinhalten höchstens einen direkten oder indirekten Aufruf von status(to) (verwendet sowohl um zu bestimmen ob die Datei existiert, als auch, für die copy_options::update_existing Option, deren letzte Schreibzeit).

Fehler wird gemeldet, wenn copy_file verwendet wird, um ein Verzeichnis zu kopieren: verwenden Sie dafür copy .

copy_file folgt symbolischen Links: verwenden Sie copy_symlink oder copy mit copy_options::copy_symlinks dafür.

Beispiel

#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");
    // jetzt gibt es zwei Dateien im Sandbox-Verzeichnis:
    std::cout << "file1.txt holds : "
              << std::ifstream("sandbox/file1.txt").rdbuf() << '\n';
    std::cout << "file2.txt holds : "
              << std::ifstream("sandbox/file2.txt").rdbuf() << '\n';
    // Fehler beim Kopieren des Verzeichnisses
    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");
}

Mögliche Ausgabe:

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

Siehe auch

gibt die Semantik von Kopiervorgängen an
(enum)
kopiert einen symbolischen Link
(function)
kopiert Dateien oder Verzeichnisse
(function)