c++ filesystem 예제

#include #include

int main() { std::filesystem::path path(“C:/example/directory”);

// Check if directory exists
if (std::filesystem::exists(path)) {
    std::cout << "Directory exists!" << std::endl;
}
else {
    std::cout << "Directory does not exist!" << std::endl;
}

// Create a new directory
std::filesystem::create_directory(path);

// Rename the directory
std::filesystem::rename(path, "C:/example/renamed_directory");

// Remove the directory
std::filesystem::remove("C:/example/renamed_directory");

return 0; } ```

이 예제 코드는 C++의 “filesystem” 라이브러리를 사용하여 디렉토리와 관련된 작업을 수행하는 방법을 보여줍니다.

먼저, std::filesystem::path를 사용하여 경로를 지정합니다. 이 예제에서는 “C:/example/directory” 경로를 사용합니다.

다음으로, std::filesystem::exists 함수를 사용하여 디렉토리가 존재하는지 확인합니다. 만약 디렉토리가 존재한다면 “Directory exists!”를 출력하고, 그렇지 않다면 “Directory does not exist!”를 출력합니다.

std::filesystem::create_directory 함수를 사용하여 새로운 디렉토리를 생성할 수 있습니다. 이 예제에서는 path에 지정된 경로에 새로운 디렉토리를 생성합니다.

std::filesystem::rename 함수를 사용하여 디렉토리의 이름을 변경할 수 있습니다. 이 예제에서는 “C:/example/renamed_directory”로 디렉토리의 이름을 변경합니다.

마지막으로, std::filesystem::remove 함수를 사용하여 디렉토리를 삭제합니다. 이 예제에서는 “C:/example/renamed_directory” 디렉토리를 삭제합니다.

위의 예제 코드를 실행하면 디렉토리의 존재 여부를 확인하고, 새로운 디렉토리를 생성하며, 디렉토리의 이름을 변경하고, 마지막으로 디렉토리를 삭제할 수 있습니다.

#C++ #filesystem ```