Namespaces
Variants

std::filesystem:: exists

From cppreference.net
헤더 파일에 정의됨 <filesystem>
bool exists ( std:: filesystem :: file_status s ) noexcept ;
(1) (C++17부터)
bool exists ( const std:: filesystem :: path & p ) ;
(2) (C++17부터)
bool exists ( const std:: filesystem :: path & p, std:: error_code & ec ) noexcept ;
(3) (C++17부터)

주어진 파일 상태나 경로가 존재하는 파일이나 디렉토리에 해당하는지 확인합니다.

1) 다음과 동일함: status_known ( s ) && s. type ( ) ! = file_type :: not_found .
2,3) s 를 각각 status ( p ) 또는 status ( p, ec ) (심링크는 따라감)로 결정된 std:: filesystem :: file_status 라고 하자. exists ( s ) 를 반환한다. 비예외 오버로드는 status_known ( s ) 일 경우 ec. clear ( ) 를 호출한다.

목차

매개변수

s - 확인할 파일 상태
p - 검사할 경로
ec - 비예외 발생 오버로드에서 오류 보고를 위한 출력 매개변수

반환값

true 주어진 경로나 파일 상태가 존재하는 파일 또는 디렉토리에 해당하는 경우, false 그렇지 않은 경우.

예외

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

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

객체가 존재하지 않으면 파일 시스템 예외가 발생하지 않습니다(반환값을 사용하세요).

참고 사항

이 함수가 제공하는 정보는 일반적으로 디렉터리 순회의 부산물로도 제공됩니다. 디렉터리 순회 중에는 exists ( * iterator ) 를 호출하는 것이 exists ( iterator - > status ( ) ) 보다 효율성이 낮습니다.

예제

#include <cstdint>
#include <filesystem>
#include <fstream>
#include <iostream>
namespace fs = std::filesystem;
void demo_exists(const fs::path& p, fs::file_status s = fs::file_status{})
{
    std::cout << p;
    if (fs::status_known(s) ? fs::exists(s) : fs::exists(p))
        std::cout << " exists\n";
    else
        std::cout << " does not exist\n";
}
int main()
{
    const fs::path sandbox{"sandbox"};
    fs::create_directory(sandbox);
    std::ofstream{sandbox/"file"}; // create regular file
    fs::create_symlink("non-existing", sandbox/"symlink");
    demo_exists(sandbox);
    for (const auto& entry : fs::directory_iterator(sandbox))
        demo_exists(entry, entry.status()); // use cached status from directory entry
    fs::remove_all(sandbox);
}

출력:

"sandbox" exists
"sandbox/symlink" does not exist
"sandbox/file" exists

참고 항목

(C++17) (C++17)
파일 속성 결정
심볼릭 링크 대상 확인하며 파일 속성 결정
(함수)
파일 유형 및 권한 표현
(클래스)
디렉토리 항목이 존재하는 파일 시스템 객체를 참조하는지 확인
( std::filesystem::directory_entry 의 public 멤버 함수)