Namespaces
Variants

std::experimental::filesystem:: copy

From cppreference.net
헤더 파일에 정의됨 <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)

파일과 디렉터리를 다양한 옵션과 함께 복사합니다:

1) 기본값으로, (2) 와 동등하며 copy_options::none options 로 사용됩니다.
2) 파일 또는 디렉토리를 from 에서 파일 또는 디렉토리 to 로 복사하며, options 에 지정된 복사 옵션을 사용합니다. options copy_options 옵션 그룹 중 어느 하나에 두 개 이상의 옵션이 존재하는 경우(심지어 copy 와 관련 없는 copy_file 그룹에서도) 동작은 정의되지 않습니다.

동작은 다음과 같습니다:

  • 우선, 다른 어떤 작업을 하기 전에, 최대 한 번의 status 호출(또는 copy_options::skip_symlinks 또는 copy_options::create_symlinks options 에 존재하는 경우 symlink_status 호출)을 통해 from 의 유형과 권한을 획득합니다.
  • 필요한 경우, 동일한 방식으로 최대 한 번의 status 또는 symlink_status 호출을 통해 to 의 상태를 획득합니다.
  • from 이 존재하지 않으면 오류를 보고합니다.
  • from to equivalent() 에 의해 동일한 파일로 판단되면 오류를 보고합니다.
  • from 또는 to 중 하나가 is_other 에 의해 일반 파일, 디렉토리 또는 심볼릭 링크가 아닌 경우 오류를 보고합니다.
  • from 이 디렉토리이지만 to 가 일반 파일인 경우 오류를 보고합니다.
  • from 이 심볼릭 링크인 경우
  • 만약 copy_options::skip_symlink options 에 존재하면, 아무 작업도 수행하지 않습니다.
  • 그렇지 않고 만약 to 가 존재하지 않으며 copy_options::copy_symlinks options 에 존재하면, copy_symlink ( from, to ) 와 같이 동작합니다.
  • 그렇지 않으면, 오류를 보고합니다.
  • 그렇지 않고, from 이 일반 파일인 경우,
  • copy_options::directories_only options 에 존재하는 경우, 아무 작업도 수행하지 않습니다.
  • 그렇지 않고 copy_options::create_symlinks options 에 존재하는 경우, to 에 대한 심볼릭 링크를 생성합니다. 참고: to 가 현재 디렉터리에 있지 않는 한 from 은 절대 경로여야 합니다.
  • 그렇지 않고 copy_options::create_hard_links options 에 존재하는 경우, to 에 대한 하드 링크를 생성합니다.
  • 그렇지 않고 to 가 디렉터리인 경우, copy_file ( from, to / from. filename ( ) , options ) 처럼 동작합니다 ( from 의 사본을 to 디렉터리 내 파일로 생성합니다).
  • 그렇지 않은 경우, copy_file ( from, to, options ) 처럼 동작합니다 (파일을 복사합니다).
  • 그렇지 않고, from 이 디렉터리이며 options copy_options::recursive 가 설정되어 있거나 copy_options::none 인 경우.
  • 만약 to 가 존재하지 않으면, 먼저 create_directory ( to, from ) 를 실행합니다 (이전 디렉토리의 속성을 복사하여 새 디렉토리를 생성합니다).
  • 그런 다음, to 가 이미 존재했든 방금 생성되었든 상관없이, from 에 포함된 파일들을 for ( const directory_entry & x : directory_iterator ( from ) ) 와 같은 방식으로 순회하며 각 디렉토리 엔트리에 대해 재귀적으로 copy ( x. path ( ) , to / x. path ( ) . filename ( ) , options | unspecified ) 를 호출합니다. 여기서 unspecified options 에 설정되어도 다른 효과가 없는 특수 비트입니다 (이 비트를 설정하는 유일한 목적은 options copy_options::none 인 경우 하위 디렉토리의 재귀적 복사를 방지하는 것입니다).
  • 그 외의 경우 아무 작업도 수행하지 않습니다.

목차

매개변수

from - 소스 파일, 디렉터리 또는 심링크의 경로
to - 대상 파일, 디렉터리 또는 심링크의 경로
ec - non-throwing 오버로드에서 오류 보고를 위한 출력 매개변수

반환값

(없음)

예외

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

참고 사항

디렉터리 복사의 기본 동작은 비재귀적 복사입니다: 파일들은 복사되지만 하위 디렉터리는 복사되지 않습니다:

// 주어진 조건
// /dir1은 /dir1/file1, /dir1/file2, /dir1/dir2를 포함
// 그리고 /dir1/dir2는 /dir1/dir2/file3을 포함
// 실행 후
std::experimental::filesystem::copy("/dir1", "/dir3");
// /dir3이 생성됨 (/dir1의 속성과 함께)
// /dir1/file1이 /dir3/file1로 복사됨
// /dir1/file2가 /dir3/file2로 복사됨

copy_options::recursive 와 함께 사용하는 동안, 하위 디렉터리도 해당 내용과 함께 재귀적으로 복사됩니다.

// ...하지만 이후에
std::experimental::filesystem::copy("/dir1", "/dir3", copy_options::recursive);
// /dir3이 생성됨 (/dir1의 속성과 함께)
// /dir1/file1이 /dir3/file1로 복사됨
// /dir1/file2이 /dir3/file2로 복사됨
// /dir3/dir2가 생성됨 (/dir1/dir2의 속성과 함께)
// /dir1/dir2/file3이 /dir3/dir2/file3로 복사됨

예제

#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"); // 파일 복사
    fs::copy("sandbox/dir", "sandbox/dir2"); // 디렉토리 복사 (비재귀적)
    // sandbox는 2개의 파일과 2개의 디렉토리를 보유하며, 그중 하나는 하위 디렉토리를 가짐
    // sandbox/file1.txt
    // sandbox/file2.txt
    // sandbox/dir2
    // sandbox/dir
    //    sandbox/dir/subdir
    fs::copy("sandbox", "sandbox/copy", fs::copy_options::recursive);
    // sandbox/copy는 위의 파일들과 하위 디렉토리들의 복사본을 보유
    fs::remove_all("sandbox");
}

참고 항목

복사 작업의 의미론을 지정함
(열거형)
심볼릭 링크를 복사함
(함수)
파일 내용을 복사함
(함수)