std::experimental::filesystem:: exists
From cppreference.net
<
cpp
|
experimental
|
fs
|
헤더 파일에 정의됨
<experimental/filesystem>
|
||
|
bool
exists
(
file_status s
)
|
(1) | (filesystem TS) |
|
bool
exists
(
const
path
&
p
)
;
bool exists ( const path & p, error_code & ec ) |
(2) | (filesystem TS) |
주어진 파일 상태나 경로가 존재하는 파일이나 디렉토리에 해당하는지 확인합니다.
1)
다음 표현과 동등함:
status_known
(
s
)
&&
s.
type
(
)
!
=
file_type
::
not_found
.
2)
다음에 해당함:
exists
(
status
(
p
)
)
또는
exists
(
status
(
p, ec
)
)
(심링크는 따름). 오류가 발생하면 비예외 오버로드는
false
를 반환합니다.
목차 |
매개변수
| s | - | 확인할 파일 상태 |
| p | - | 검사할 경로 |
| ec | - | 비예외 발생 오버로드에서 오류 보고를 위한 출력 매개변수 |
반환값
true 주어진 경로나 파일 상태가 존재하는 파일 또는 디렉토리에 해당하는 경우, false 그렇지 않은 경우.
예외
1)
noexcept
명세:
noexcept
2)
error_code
&
매개변수를 받지 않는 오버로드는 기본 OS API 오류 시
filesystem_error
를 발생시키며, 첫 번째 인수로
p
를, 오류 코드 인수로 OS 오류 코드를 사용하여 구성됩니다.
std::
bad_alloc
는 메모리 할당이 실패할 경우 발생할 수 있습니다.
error_code
&
매개변수를 받는 오버로드는 OS API 호출이 실패할 경우 이를 OS API 오류 코드로 설정하고, 오류가 발생하지 않으면
ec.
clear
(
)
를 실행합니다. 이 오버로드는
noexcept
사양을 가집니다:
noexcept
참고 사항
이 함수가 제공하는 정보는 일반적으로 디렉터리 순회의 부산물로도 제공됩니다. 디렉터리 순회 중에는
exists(*iterator)
를 호출하는 것이
exists(iterator->status())
를 호출하는 것보다 효율성이 떨어집니다.
예제
이 코드 실행
#include <cstdint> #include <experimental/filesystem> #include <fstream> #include <iostream> namespace fs = std::experimental::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() { fs::create_directory("sandbox"); std::ofstream("sandbox/file"); // create regular file fs::create_symlink("non-existing", "sandbox/symlink"); demo_exists("sandbox"); for (auto it = fs::directory_iterator("sandbox"); it != fs::directory_iterator(); ++it) demo_exists(*it, it->status()); // use cached status from directory entry fs::remove_all("sandbox"); }
출력:
"sandbox" exists "sandbox/file" exists "sandbox/symlink" does not exist
참고 항목
|
파일 속성 결정
심볼릭 링크 대상 확인하며 파일 속성 결정 (함수) |
|
|
파일 유형 및 권한 표현
(클래스) |
|
|
이 디렉토리 항목이 지정하는 파일의 캐시된 상태
이 디렉토리 항목이 지정하는 파일의 캐시된 symlink_status (
std::experimental::filesystem::directory_entry
의
공개 멤버 함수)
|