본문 바로가기

C++

std::filesystem

visual studio 에서 std::filesystem 사용을 위해선 다음과 같은 설정이 필요하다.

#include <iostream>
#include <filesystem>

int main()
{
	const auto print_value = [](auto v) {
		std::cout << v << std::endl;
	};

	std::filesystem::path path0("D:/a/b/c/d");
	while (!std::filesystem::exists(path0)) {
		print_value(path0.string() + " is not exist");
		if (std::filesystem::create_directories(path0)) {
			print_value(path0.string() + " is created");
		}
	}

	std::filesystem::path org = std::filesystem::current_path();
	std::cout << "Current Working directory "; print_value(org);
	std::filesystem::current_path(std::filesystem::path("C:/"));
	std::cout << "Current Working directory "; print_value(std::filesystem::current_path());
	std::filesystem::current_path(org);

	std::filesystem::path path1("filesystem.cpp");

	if (std::filesystem::exists(path1)) {
		print_value(std::filesystem::absolute(path1));
		print_value(std::filesystem::canonical(path1));
	}

	std::filesystem::path path2("noexist.cpp");
	if (!std::filesystem::exists(path2)) {
		print_value("path2 is not exist");
	}

	std::filesystem::path path3("./");
	
	path3.append("abc.txt");
	print_value(path3);

	auto t = std::filesystem::absolute(path3);

	print_value(t.parent_path());

	return 0;
}

출력




'C++' 카테고리의 다른 글

STL priority_queue  (0) 2021.10.28
가변인자 템플릿  (0) 2021.10.27
std::shared_ptr  (0) 2021.10.27
std::unique_ptr  (0) 2021.10.27
std::find()  (0) 2021.10.27