Namespaces
Variants

std::filesystem:: copy_file

From cppreference.net
헤더 파일에 정의됨 <filesystem>
bool copy_file ( const std:: filesystem :: path & from,
const std:: filesystem :: path & to ) ;
(1) (C++17부터)
bool copy_file ( const std:: filesystem :: path & from,

const std:: filesystem :: path & to,

std:: error_code & ec ) ;
(2) (C++17부터)
(3) (C++17부터)
(4) (C++17부터)
1,2) 기본값으로, (3,4) 와 동일하며 copy_options::none options 로 사용됩니다.
3,4) from 에서 to 로 단일 파일을 복사하며, options 에 지정된 복사 옵션을 사용합니다. options copy_options 옵션 그룹 중 어느 하나라도 둘 이상의 옵션이 존재하면 (심지어 filesystem::copy_file 와 관련 없는 그룹이라도) 동작은 정의되지 않습니다.
  • 만약 !filesystem::is_regular_file(from) 인 경우 (소스 파일이 존재하지 않거나 일반 파일이 아니기 때문), 오류를 보고합니다.
  • 그렇지 않고 대상 파일이 존재하지 않는 경우,
  • from 이 확인되는 파일의 내용과 속성을 to 가 확인되는 파일로 복사합니다 (심볼릭 링크는 따라갑니다).
  • 그렇지 않고 대상 파일이 이미 존재하는 경우,
  • 다음 중 어느 하나라도 참이면 오류를 보고합니다:
  • 그렇지 않고 copy_options::skip_existing options 에 설정된 경우, 아무 작업도 수행하지 않습니다.
  • 그렇지 않고 copy_options::overwrite_existing options 에 설정된 경우, from 이 확인되는 파일의 내용과 속성을 to 가 확인되는 파일로 복사합니다.
  • 그렇지 않고 copy_options::update_existing options 에 설정된 경우, from to 보다 최신인 경우에만 파일을 복사하며, 이는 filesystem::last_write_time() 에 의해 정의됩니다.

비예외(non-throwing) 오버로드는 오류가 발생할 경우 false 를 반환합니다.

목차

매개변수

from - 소스 파일 경로
to - 대상 파일 경로
ec - non-throwing 오버로드에서 오류 보고를 위한 out-parameter

반환값

true 파일이 복사된 경우, false 그렇지 않은 경우.

예외

noexcept 로 표시되지 않은 모든 오버로드는 메모리 할당이 실패할 경우 std::bad_alloc 을(를) throw할 수 있습니다.

1,3) 기본 OS API 오류 발생 시 std::filesystem::filesystem_error 를 발생시킵니다. 이는 from 을 첫 번째 경로 인수로, to 을 두 번째 경로 인수로, OS 오류 코드를 에러 코드 인수로 사용하여 구성됩니다.
2,4) OS API 호출이 실패할 경우 std:: error_code & 매개변수를 OS API 오류 코드로 설정하고, 오류가 발생하지 않을 경우 ec. clear ( ) 를 실행합니다.

참고 사항

함수들은 최대 한 번의 직접 또는 간접 호출을 포함하며, filesystem::status(to) (파일 존재 여부 확인과 filesystem::copy_options::update_existing 옵션의 경우 마지막 수정 시간 확인에 모두 사용됨)를 호출합니다.

디렉터리를 복사할 때 filesystem::copy_file 을 사용하면 오류가 보고됩니다: 해당 작업에는 filesystem::copy 를 사용하십시오.

filesystem::copy_file 는 심볼릭 링크를 따릅니다: 이를 위해서는 filesystem::copy_symlink 또는 filesystem::copy 와 함께 filesystem::copy_options::copy_symlinks 를 사용하십시오.

예제

#include <filesystem>
#include <fstream>
#include <iostream>
namespace fs = std::filesystem;
int main()
{
    fs::create_directory("sandbox");
    std::ofstream("sandbox/file1.txt").put('a');
    fs::copy_file("sandbox/file1.txt", "sandbox/file2.txt");
    // 이제 sandbox에 두 개의 파일이 있습니다:
    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"

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 3014 C++17 error_code 오버로드가 noexcept로 표시되었으나 메모리 할당 가능 noexcept 제거됨

참고 항목

복사 작업의 의미론을 지정함
(enum)
심볼릭 링크를 복사함
(function)
(C++17)
파일 또는 디렉토리를 복사함
(function)