Programming Language/C,C++

[C++] 파일 존재 확인

깜태 2021. 12. 14. 13:47
728x90

 

1. 윈도우면 #include <io.h> 리눅스면 #include <unistd.h>,

2. access(filename, mode) 함수 사용하거나, 보안문제로 _access 함수 사용

(mode = 0 : 존재, 2 : 쓰기 가능, 4 : 읽기 가능, 6: 읽기 및 쓰기 )

 

if(access(filename, 0) != -1)
{
	std::cout<<filename << "exists" << std::endl;
}
else
{
	std::cout<<filename << "doesn't exists" << std::endl;
}

 

 

참고

https://docs.microsoft.com/ko-kr/cpp/c-runtime-library/reference/access-waccess?view=msvc-170

https://junh0.tistory.com/8

https://maincodes.tistory.com/4

728x90