std::filesystem::create_directory / create_directories

std::filesystem::create_directory / create_directories (디렉터리 생성)

디렉터리를 하나 만들거나(create_directory), 경로의 모든 요소에 대해 재귀적으로 만드는(create_directories) 함수예요. C++17부터 있어요.

출처: cppreference

본문

<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,
                       std::error_code& ec ) noexcept;               // (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
  • 1,2) POSIX mkdir()처럼 두 번째 인자를 static_cast<int>(std::filesystem::perms::all)로 해서 디렉터리 p를 만들어요(부모 디렉터리는 이미 존재해야 해요). p가 기존 디렉터리로 귀결되어 실패하면 오류를 보고하지 않아요. 그 외의 실패에는 오류를 보고해요.
  • 3,4) (1,2)와 같되, 새 디렉터리의 속성을 existing_p(존재하는 디렉터리여야 해요)에서 복사해요. 어떤 속성이 복사될지는 OS에 따라 달라요. POSIX에서는 다음처럼 속성을 복사해요.
    stat(existing_p.c_str(), &attributes_stat)
    mkdir(p.c_str(), attributes_stat.st_mode)
    
    Windows에서는 existing_p의 속성이 복사되지 않아요.
  • 5,6) p에서 아직 존재하지 않는 모든 요소에 대해 (1,2)를 실행해요. p가 이미 존재하면 아무것도 하지 않고 성공을 보고해요. p의 요소 하나라도 생성에 실패하면 오류를 보고해요.

매개변수

  • p — 새 디렉터리의 경로
  • existing_p — (3,4)에서 새 디렉터리 속성의 원본이 될 경로
  • ec — 예외 없는 버전에서 오류 보고용 출력 매개변수

반환값

디렉터리를 만들었으면 true, 이미 존재해서 만들 필요가 없었으면 false예요. 예외 없는 버전은 오류 시 false를 돌려줘요.

예외

던지는 버전은 OS API 오류가 있으면 filesystem_error를 던져요. 예외 없는 버전은 ec를 설정해요.

더 알아보기 (Learn more)

cppreference