[c++] 파일 및 디렉토리 위치 정보 확인하기
먼저, <filesystem>
헤더를 포함한 후에 다음과 같은 코드를 사용하여 파일 또는 디렉토리의 위치 정보를 확인할 수 있습니다.
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main() {
fs::path filePath = "example.txt";
if (fs::exists(filePath)) {
std::cout << "File exists." << std::endl;
std::cout << "File path: " << filePath << std::endl;
std::cout << "File size: " << fs::file_size(filePath) << " bytes" << std::endl;
std::cout << "Last write time: " << fs::last_write_time(filePath) << std::endl;
} else {
std::cout << "File does not exist." << std::endl;
}
return 0;
}
위의 코드에서는 <filesystem>
헤더를 사용하여 파일이 존재하고 있다면 파일 경로, 크기 및 최종 수정 시간을 출력하고 있습니다.
이러한 함수와 클래스들은 C++17 이상에서 사용 가능하며, 파일 시스템 작업을 보다 쉽게 처리할 수 있다는 장점이 있습니다.
참고 문헌:
- https://en.cppreference.com/w/cpp/header/filesystem