C++ 개념 정리

30. filesystem

CE : 하랑 2026. 3. 2. 11:31

std::filesystem::path이 뭔지를 파악하고 경로와 관련된 편리한 기능들을 제공한다.
경로라는것은 근본적으로 보자면 "C:\\aaaa\bbb\\ccc" => 문자열에 가깝다.

이 경로를 담당하는 클래스는 분명히 내부에 std::string을 리턴해주는 함수가 있어야 한다.

 

 

1. std::filesystem::current_path() -> 현재 경로를 리턴

class UEnginePath
{
public: // 맴버이니셜 라이저 초기화
	UEnginePath() : Path(std::filesystem::current_path()) // 현재 경로
	{

	};

	std::filesystem::path GetPath()
	{
		return Path.string(); // std::string으로 반환
	}

private:
	std::filesystem::path Path;
};

 

 

2. std::filesystem::exists() 함수는 지정한 경로의 파일이나 디렉토리가 존재하는지를 확인하는 함수

void Move(std::string_view _Path)
{
	std::filesystem::path NextPath = Path;
	NextPath.append(_Path); // 경로 추가

	bool Check = std::filesystem::exists(NextPath);

	if (false == Check)
	{
		std::cout << "경로 틀림";
	}
	else
	{
		std::cout << "경로 존재";
	}
}

 

 

3. parent_path() -> 상위 디렉토리 경로를 얻음

void MoveParent()
{
	Path = Path.parent_path();
};

 

 

4. extension() -> 현재 경로명의 확장자이거나 확장자가 없는 경우 빈 경로입니다.

std::string GetExtension() const
{
	std::filesystem::path Text = Path.extension();

	return Text.string();
};

test.Move("a.txt");
std::cout << test.GetExtension();

 

 

5. filename() -> 폴더명이나 파일명 반환

std::string GetFileName() const
{
	std::filesystem::path Text = Path.filename();
	return Text.string();
};

 

 

6. root_path() -> 최상위 (루트) 경로 반환

std::string GetRootPath() const
{
	return Path.root_path().string();
};

 

 

7. std::filesystem::file_size() -> 파일 사이즈 반환

int GetFilesize()
{
	return static_cast<int>(std::filesystem::file_size(Path));
}


폴더인경우는 -> 폴더안의 전체 파일 크기

 

파일은 파일만

 

'C++ 개념 정리' 카테고리의 다른 글

32. memcpy  (0) 2026.03.02
31. 파일 입출력  (0) 2026.03.02
29. namespace  (0) 2026.03.02
27. static VS extern  (0) 2026.03.01
26. 비트 마스크 열거형(enum)  (0) 2026.02.08