std::experimental::filesystem:: copy
From cppreference.net
<
cpp
|
experimental
|
fs
|
Definiert im Header
<experimental/filesystem>
|
||
|
void
copy
(
const
path
&
from,
const
path
&
to
)
;
void copy ( const path & from, const path & to, error_code & ec ) ; |
(1) | (filesystem TS) |
|
void
copy
(
const
path
&
from,
const
path
&
to, copy_options options
)
;
void copy ( const path & from, const path & to, copy_options options, error_code & ec ) ; |
(2) | (filesystem TS) |
Kopiert Dateien und Verzeichnisse mit verschiedenen Optionen:
1)
Die Standardeinstellung, entspricht
(2)
mit
copy_options::none
verwendet als
options
.
2)
Kopiert die Datei oder das Verzeichnis
from
zur Datei oder zum Verzeichnis
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 der
copy_file
Gruppe, die für
copy
nicht relevant ist).
Das Verhalten ist wie folgt:
-
Zuerst, bevor irgendetwas anderes unternommen wird, ermittelt den Typ und die Berechtigungen von
from
durch nicht mehr als einen einzigen Aufruf von
status
(oder, falls
copy_options::skip_symlinksodercopy_options::create_symlinksin options vorhanden sind, durch einen Aufruf vonsymlink_status). - Falls notwendig, ermittelt den Status von to auf die gleiche Weise, durch nicht mehr als einen einzigen status- oder symlink_status-Aufruf.
- Wenn from nicht existiert, wird ein Fehler gemeldet.
- Wenn from und to dieselbe Datei sind, wie durch equivalent() bestimmt, wird ein Fehler gemeldet.
- Wenn entweder from oder to keine reguläre Datei, kein Verzeichnis und kein symbolischer Link ist, wie durch is_other bestimmt, wird ein Fehler gemeldet.
- Wenn from ein Verzeichnis ist, aber to eine reguläre Datei ist, wird ein Fehler gemeldet.
- Wenn from ein symbolischer Link ist, dann
-
-
Wenn
copy_options::skip_symlinkin options vorhanden ist, tut nichts. -
Andernfalls, wenn
to
nicht existiert und
copy_options::copy_symlinksin options vorhanden ist, verhält sich wie copy_symlink ( from, to ) . - Andernfalls wird ein Fehler gemeldet.
-
Wenn
- Andernfalls, wenn from eine reguläre Datei ist, dann
-
-
Falls
copy_options::directories_onlyin options vorhanden ist, erfolgt keine Aktion. -
Andernfalls, falls
copy_options::create_symlinksin options vorhanden ist, wird eine symbolische Verknüpfung zu to erstellt. Hinweis: from muss ein absoluter Pfad sein, es sei denn to befindet sich im aktuellen Verzeichnis. -
Andernfalls, falls
copy_options::create_hard_linksin options vorhanden ist, wird eine feste Verknüpfung zu to erstellt. - Andernfalls, falls to ein Verzeichnis ist, verhält es sich wie copy_file ( from, to / from. filename ( ) , options ) (erstellt eine Kopie von from als Datei im Verzeichnis to ).
- Andernfalls verhält es sich wie copy_file ( from, to, options ) (kopiert die Datei).
-
Falls
-
Andernfalls, wenn
from
ein Verzeichnis ist und entweder
options
copy_options::recursiveenthält odercopy_options::noneist.
-
- Falls to nicht existiert, wird zunächst create_directory ( to, from ) ausgeführt (erstellt das neue Verzeichnis mit einer Kopie der Attribute des alten Verzeichnisses).
-
Dann, unabhängig davon ob
to
bereits existierte oder gerade erstellt wurde, werden die Dateien in
from
durchlaufen, als ob durch
for
(
const
directory_entry
&
x
:
directory_iterator
(
from
)
)
, und für jeden Verzeichniseintrag wird rekursiv
copy
(
x.
path
(
)
, to
/
x.
path
(
)
.
filename
(
)
, options
|
unspecified
)
aufgerufen, wobei
unspecified
ein spezielles Bit ist, das keine andere Wirkung hat, wenn es in
options
gesetzt wird (der einzige Zweck der Setzung dieses Bits ist, rekursives Kopieren von Unterverzeichnissen zu verhindern, falls
options
gleich
copy_options::noneist).
- Andernfalls tut nichts.
Inhaltsverzeichnis |
Parameter
| from | - | Pfad zur Quelldatei, zum Verzeichnis oder zur symbolischen Verknüpfung |
| to | - | Pfad zur Zieldatei, zum Verzeichnis oder zur symbolischen Verknüpfung |
| ec | - | Out-Parameter für Fehlerberichterstattung in der nicht-werfenden Überladung |
Rückgabewert
(keine)
Exceptions
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
Das Standardverhalten beim Kopieren von Verzeichnissen ist das nicht-rekursive Kopieren: Die Dateien werden kopiert, aber nicht die Unterverzeichnisse:
// Gegeben // /dir1 enthält /dir1/file1, /dir1/file2, /dir1/dir2 // und /dir1/dir2 enthält /dir1/dir2/file3 // Nach std::experimental::filesystem::copy("/dir1", "/dir3"); // /dir3 wird erstellt (mit den Attributen von /dir1) // /dir1/file1 wird nach /dir3/file1 kopiert // /dir1/file2 wird nach /dir3/file2 kopiert
Während mit
copy_options::recursive
die Unterverzeichnisse ebenfalls kopiert werden, inklusive ihres Inhalts, rekursiv.
// ...aber nach std::experimental::filesystem::copy("/dir1", "/dir3", copy_options::recursive); // /dir3 wird erstellt (mit den Attributen von /dir1) // /dir1/file1 wird nach /dir3/file1 kopiert // /dir1/file2 wird nach /dir3/file2 kopiert // /dir3/dir2 wird erstellt (mit den Attributen von /dir1/dir2) // /dir1/dir2/file3 wird nach /dir3/dir2/file3 kopiert
Beispiel
Diesen Code ausführen
#include <experimental/filesystem> #include <fstream> #include <iostream> namespace fs = std::experimental::filesystem; int main() { fs::create_directories("sandbox/dir/subdir"); std::ofstream("sandbox/file1.txt").put('a'); fs::copy("sandbox/file1.txt", "sandbox/file2.txt"); // Datei kopieren fs::copy("sandbox/dir", "sandbox/dir2"); // Verzeichnis kopieren (nicht rekursiv) // sandbox enthält 2 Dateien und 2 Verzeichnisse, eines davon mit Unterverzeichnis // sandbox/file1.txt // sandbox/file2.txt // sandbox/dir2 // sandbox/dir // sandbox/dir/subdir fs::copy("sandbox", "sandbox/copy", fs::copy_options::recursive); // sandbox/copy enthält Kopien der obigen Dateien und Unterverzeichnisse fs::remove_all("sandbox"); }
Siehe auch
|
gibt die Semantik von Kopiervorgängen an
(enum) |
|
|
kopiert einen symbolischen Link
(function) |
|
|
kopiert Dateiinhalte
(function) |