std::filesystem:: copy_file
|
헤더 파일에 정의됨
<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,
|
(2) | (C++17부터) |
|
bool
copy_file
(
const
std::
filesystem
::
path
&
from,
const
std::
filesystem
::
path
&
to,
|
(3) | (C++17부터) |
|
bool
copy_file
(
const
std::
filesystem
::
path
&
from,
const
std::
filesystem
::
path
&
to,
|
(4) | (C++17부터) |
copy_options::none
가
options
로 사용됩니다.
- 만약 !filesystem::is_regular_file(from) 인 경우 (소스 파일이 존재하지 않거나 일반 파일이 아니기 때문), 오류를 보고합니다.
- 그렇지 않고 대상 파일이 존재하지 않는 경우,
-
- from 이 확인되는 파일의 내용과 속성을 to 가 확인되는 파일로 복사합니다 (심볼릭 링크는 따라갑니다).
- 그렇지 않고 대상 파일이 이미 존재하는 경우,
-
- 다음 중 어느 하나라도 참이면 오류를 보고합니다:
-
- to 와 from 이 filesystem::equivalent(from, to) 로 판단될 때 동일한 경우;
- to 가 !filesystem::is_regular_file(to) 로 판단될 때 일반 파일이 아닌 경우;
- options 에 filesystem::copy_file 제어 옵션이 하나도 설정되지 않은 경우.
-
그렇지 않고
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할 수 있습니다.
참고 사항
함수들은 최대 한 번의 직접 또는 간접 호출을 포함하며,
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 제거됨 |
참고 항목
|
(C++17)
|
복사 작업의 의미론을 지정함
(enum) |
|
(C++17)
|
심볼릭 링크를 복사함
(function) |
|
(C++17)
|
파일 또는 디렉토리를 복사함
(function) |