Namespaces
Variants

std::filesystem::directory_entry:: exists

From cppreference.net
bool exists ( ) const ;
(1) (C++17 이후)
bool exists ( std:: error_code & ec ) const noexcept ;
(2) (C++17 이후)

가리키는 객체가 존재하는지 확인합니다. 효과적으로 다음을 반환합니다:

1) std:: filesystem :: exists ( status ( ) ) ,
2) std:: filesystem :: exists ( status ( ec ) ) .

주의할 점은 status() 가 심볼릭 링크를 따라 대상으로 이동한다는 것입니다.

목차

매개변수

ec - 비예외 발생 오버로드에서 오류 보고를 위한 출력 매개변수

반환값

true 참조된 파일 시스템 객체가 존재하는 경우.

예외

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

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

예제

#include <filesystem>
#include <iostream>
int main()
{
    for (auto const str:
    {
        "/usr/bin/cat",
        "/usr/bin/mouse",
        "/usr/bin/python",
        "/usr/bin/bison",
        "/usr/bin/yacc",
        "/usr/bin/c++",
    })
    {
        std::filesystem::directory_entry entry{str};
        std::cout << "directory entry " << entry
                  << (entry.exists() ? " exists\n" : " does not exist\n");
    }
}

가능한 출력:

// POSIX 시스템에서의 출력:
directory entry "/usr/bin/cat" exists
directory entry "/usr/bin/mouse" does not exist
directory entry "/usr/bin/python" exists
directory entry "/usr/bin/bison" exists
directory entry "/usr/bin/yacc" does not exist
directory entry "/usr/bin/c++" exists

참고 항목

(C++17)
경로가 존재하는 파일 시스템 객체를 참조하는지 확인합니다
(함수)