[C++] 파일시스템

Date:     Updated:

카테고리:

태그:

이 글은 C++의 파일시스템을 공부하고 정리한 글입니다.
파일시스템은 파일 속성 변경, 디렉터리 순회, 파일 복사 등에 관한 라이브러리이다.

파일시스템

  • C++17의 새로운 라이브러리
  • 파일 읽기와 쓰기에 관한 라이브러리가 아님
  • 파일 속성 변경, 디렉터리 순회, 파일 복사 등에 관한 라이브러리
  • 이 모든 걸 std::fs로 할 수 있음

파일시스템 연산

  • 플랫품 공통적인 방법으로 경로 합치기
  • 파일과 디렉터리를 복사, 이름 바꾸기, 삭제
  • 디렉터리에서 파일, 디렉터리 목록 가져오기
  • 파일 권한 읽기 및 설정
  • 파일 상태 읽기 및 설정

    std::filesystem::path::operator/=

    path& operator/=(const path& p);
    
    • p를 현재 경로의 서브 폴더로 덧붙인다
    • 리눅스와 윈도우 모두에서 작동
    std::filesystem::path path1 = "D:\\Lecture";
    path1 /= "examples";                              // D:\Lecture\examples
      
    std::filesystem::path path2 = "D:\\Lecture";
    path2 /= "/examples";                              // D:\examples
    

    std::filesystem::copy()

    void copy( const std::filesystem::path& from, const std::filesystem::path& to );
    void copy( const std::filesystem::path& from, const std::filesystem::path& to, std::filesystem::copy_options options );
    
    • 파일 또는 디렉터리를 복사한다
    // fs::path originalDirPath = "C:\\examples\\folder1";
    // fs::path copiedDirPath1 = "C:\\examples\\copiedFolder1";
    // fs::path copiedDirpath2 = "C:\\examples\\copiedFolder2";
    
    fs::copy(originalDirPath, copiedDirPath1);                                // 디렉터리 복사 (비재귀적으로)
    fs::copy(originalDirPath, copiedDirpath2, fs::copy_options::recursive);   // 디렉터리 복사 (재귀적으로)
    

    fs::copy_options::recursive

    • 복사를 할 때 모든 서브 폴더도 복사한다

    std::filesystem::rename()

    void rename( const std::filesystem::path& old_p, const std::filesystem::path& new_p );
    
    • 파일 또는 디렉터리의 이름을 바꾸거나 이동시킨다
    // fs::path filePath = "C:\\examples\\myRank.txt";
    // fs::path renamedPath = "C:\\examples\\folder1\\rank.txt";
    
    fs::rename(filePath, renamedPath);
    

    std::filesystem::remove() / remove_all()

    1. bool remove( const std::filesystem::path& p);
    
    2. std::uintmax_t remove_all( const std::filesystem::path& p);
    
    1. 파일이나 빈 디렉터리를 삭제한다
    2. p 안에 들어 있는 내용물들을 삭제한다
    fs::remove("C:\\example\\myRank.txt");    // myRank.txt를 삭제
    
    fs::remove_all("C:\\example");            // example 폴더 안에 있는 모든 것을 삭제
    

    std::filesystem::recursive_directory_iterator()

    class recursive_directory_iterator;
    
    • 디렉터리의 요소들을 순회한다
    • 그리고 재귀적으로 하위 디렉터리의 요소들을 순회한다
    for (auto& path : fs::recursive_directory_iterator("C:\\Lecture\\FilesystemExample"))
    {
      std::cout << path << std::endl;
    }
    

    std::filesystem::status()

    std::filesystem::file_status status(const std::filesystem::path& p);
    
    • 파일 상태를 반환한다
      • 블록 파일(block file), 디렉터리, FIFO, 소켓(socket), …
    std::filesystem::file_status status = std::filesystem::status("C:\\example");
    

    std::filesystem::permissions()

    perms permissions();
    
    • 파일 권한을 반환한다
      • 소유자의 읽기, 쓰기, 실행
      • 소유자와 같은 그룹의 읽기, 쓰기, 실행
      • 외부인의 읽기, 쓰기, 실행
    std::filesystem::perms permissions = std::filesystem::status("C:\\examples\\folder1\\rank.txt").permissions();
    


참조

포큐아카데미 C++ 언매니지드 프로그래밍



💻 열심히 공부해서 작성 중이니 오류나 틀린 부분이 있을 경우 
  언제든지 댓글 혹은 메일로 알려주시면 감사하겠습니다! 😸

맨 위로 이동하기

Cpp 카테고리 내 다른 글 보러가기

댓글 남기기