std::filesystem:: create_directory, std::filesystem:: create_directories
|
헤더 파일에 정의됨
<filesystem>
|
||
|
bool
create_directory
(
const
std::
filesystem
::
path
&
p
)
;
|
(1) | (C++17부터) |
|
bool
create_directory
(
const
std::
filesystem
::
path
&
p,
std::
error_code
&
ec
)
noexcept
;
|
(2) | (C++17부터) |
|
bool
create_directory
(
const
std::
filesystem
::
path
&
p,
const std:: filesystem :: path & existing_p ) ; |
(3) | (C++17부터) |
|
bool
create_directory
(
const
std::
filesystem
::
path
&
p,
const
std::
filesystem
::
path
&
existing_p,
|
(4) | (C++17부터) |
|
bool
create_directories
(
const
std::
filesystem
::
path
&
p
)
;
|
(5) | (C++17부터) |
|
bool
create_directories
(
const
std::
filesystem
::
path
&
p,
std::
error_code
&
ec
)
;
|
(6) | (C++17부터) |
mkdir()
함수를 두 번째 인자로
static_cast
<
int
>
(
std::
filesystem
::
perms
::
all
)
를 사용하여 (상위 디렉토리는 반드시 이미 존재해야 함). 만약
p
가 기존 디렉토리로 확인되어 함수가 실패하면, 오류가 보고되지 않습니다. 그 외의 실패 시에는 오류가 보고됩니다.
stat(existing_p.c_str(), &attributes_stat) mkdir(p.c_str(), attributes_stat.st_mode)
목차 |
매개변수
| p | - | 생성할 새 디렉터리의 경로 |
| existing_p | - | 속성을 복사할 디렉터리의 경로 |
| ec | - | non-throwing 오버로드에서 오류 보고를 위한 출력 매개변수 |
반환값
true 디렉토리 p 가 새로 생성된 경우, false 그렇지 않은 경우.
예외
noexcept
로 표시되지 않은 모든 오버로드는 메모리 할당이 실패할 경우
std::bad_alloc
을(를) throw할 수 있습니다.
참고 사항
속성 보존 오버로드
(3,4)
는 디렉터리를 재귀적으로 복사할 때
copy()
에 의해 암시적으로 호출됩니다. boost.filesystem에서 이에 상응하는 함수는
copy_directory
입니다(인수 순서가 반대).
예제
#include <cassert> #include <cstdlib> #include <filesystem> int main() { std::filesystem::current_path(std::filesystem::temp_directory_path()); // 기본 사용법 std::filesystem::create_directories("sandbox/1/2/a"); std::filesystem::create_directory("sandbox/1/2/b"); // 디렉토리가 이미 존재함 (false 반환, 오류 없음) assert(!std::filesystem::create_directory("sandbox/1/2/b")); // 권한 복사 사용법 std::filesystem::permissions( "sandbox/1/2/b", std::filesystem::perms::others_all, std::filesystem::perm_options::remove ); std::filesystem::create_directory("sandbox/1/2/c", "sandbox/1/2/b"); std::system("ls -l sandbox/1/2"); std::system("tree sandbox"); std::filesystem::remove_all("sandbox"); }
가능한 출력:
drwxr-xr-x 2 user group 4096 Apr 15 09:33 a
drwxr-x--- 2 user group 4096 Apr 15 09:33 b
drwxr-x--- 2 user group 4096 Apr 15 09:33 c
sandbox
└── 1
└── 2
├── a
├── b
└── c
결함 보고서
다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 2935 | C++17 | 대상이 이미 존재하지만 디렉토리가 아닌 경우 오류 | 오류 아님 |
| LWG 3014 | C++17 |
error_code
오버로드의
create_directories
가 noexcept로 지정되었으나 메모리 할당 가능
|
noexcept 제거됨 |
| P1164R1 | C++17 | 기존 비디렉토리 파일로 인한 생성 실패가 오류가 아님 | 오류로 지정됨 |
참고 항목
|
(C++17)
(C++17)
|
심볼릭 링크를 생성함
(함수) |
|
(C++17)
|
파일 또는 디렉토리를 복사함
(함수) |
|
(C++17)
|
파일 시스템 권한을 식별함
(열거형) |