[C++기초] 19. 파일시스템, 모듈시스템

INDEX

  1. 파일 시스템
  2. 모듈 시스템

파일 시스템

파일 시스템

파일시스템 연산

예시 : 경로 합치기

#include<filesystem>

//컴파일러에 따라 std::filesystem 네임스페이스 대신 std::experimental::filesystem::v1을 사용해야 될 수도 있음
namespace fs = std::experimental::filesystem::v1;

int main()
{
	fs::path path1 = "D:\\Lecture;"
	fs::path paht2 = "example";
	path1 /= path2;
	
	fs::path path3 = "D:\\Lecture;"
	fs::path paht4 = "example";
	path += path4;
	
	return 0;
}

iamge

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

path& operator/=(const path& p)

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);
  • 파일 또는 디렉터리를 복사한다

예시 : 파일 또는 디렉터리 복사하기

#include <filesystem>
namespace fs = std::experimental::filesystem::v1;
int main()
{
    fs::path originalTextPath = "C:\\examples\\myRank.txt";
    fs::path copiedTextPath = "C:\\examples\\copiedMyRank.txt";

    fs::path originalDirPath = "C:\\examples\\folder1";
    fs::path copiedDirPath1 = "C:\\examples\\copiedfolder1";
    fs::path copiedDirPath2 = "C:\\examples\\copiedfolder2";

    fs::copy(originalTextPath, copiedTextPath); // 파일 복사
    fs::copy(originalDirPath, copiedDirPath1); // 디렉터리 복사 (비재귀적으로)
    fs::copy(originalDirPath, copiedDirPath2, copy_options::recursive); // 디렉터리 복사 (재귀적으로)

    return 0;
}

std::filesystem::rename()

void rename( const std::filesystem::path& old_p, const std::filesystem::path& new_p);

예시 : 파일 또는 디렉터리 이름 바꾸기/이동

#include <experimental/filesystem>

namespace fs = std::experimental::filesystem::v1;
int main()
{
    fs::path filePath = "C:\\examples\\myRank.txt";
    fs::path renamedPath = "C:\\examples\\folder1\\rank.txt";
    fs::rename(filePath, renamedPath);

    return 0;
}

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

bool remove(const std::filesystem::path& p);
std::uintmax_t remove_all(const std::filesystem::paht& p);
fs::remove("C:\\examples\\myRank.txt"); // myRank.txt를 삭제
fs::remove_all("C:\\examples"); // example 폴더 안에 있는 모든 것을 삭제

예시 : 파일 또는 디렉터리 삭제

#include <experimental/filesystem>
namespace fs = std::experimental::filesystem::v1;
int main()
{
    fs::path currentPath = fs::current_path();
    fs::create_directiories(currentPath / "data");//프로젝트 폴더에 새 폴더를 만듦
    fs::remove(correntPath / "data");

    return 0;
}

std::filesystem::recursive_directory_iterator()

class recursive_directory_iterator;

예시 : 디렉터리의 파일 목록 가져오기

#include <experimental/filesystem>
namespace fs = std::experimental::filesystem::v1;

int main()
{
	for(auto& path : fs::recursive_directory_iterator("C:\\Lecture\\FilesystemExample"))
	{
		std::cout << path <<std::endl;
	}
	
	return 0;
}

Result

image

std::filesystem::status()

std::filesystem::file_status status(const std::filesystem::path& p);

std::filesystem::permissions()

perms permissions();

예시 : 파일 속성(권한) 읽기

#include <experimental/filesystem>
namespace fs = std::experimental::filesystem::v1;

void PrintPermission(fs:;perms permission)
{
    std::cout<<(permission & fs::perms::owner_read) != fs::perms::none ? "r" : "-")
        <<((permission & fs::perms::owner_write) != fs::perms::none ? "w" : "-")
        <<((permission & fs::perms::owner_exec) != fs::perms::none ? "x" : "-")
        <<((permission & fs::perms::group_read) != fs::perms::none ? "r" : "-")
        <<((permission & fs::perms::group_write) != fs::perms::none ? "w" : "-")
        <<((permission & fs::perms::group_exec) != fs::perms::none ? "x" : "-")
        <<((permission & fs::perms::others_read) != fs::perms::none ? "r" : "-")
        <<((permission & fs::perms::others_write) != fs::perms::none ? "w" : "-")
        <<((permission & fs::perms::others_exec) != fs::perms::none ? "x" : "-")
        << std::endl;
}
int main()
{
    fs::path filePath = "C:\\examples\\folder1\\rank.txt";
    PrintPermission(fs::status(filePath).permissions());

    return 0;
}  

Result

image

모듈 시스템

모듈 시스템