반응형
파일시스템 관련 C++ 표준 함수
#include <iostream>
#include <fstream>
#include <filesystem> // C++17 이상에서 지원됨
namespace fs = std::filesystem;
int main() {
// 파일 생성
fs::path filePath = "example.txt";
std::ofstream outputFile(filePath); // 파일스트림을 이용해 파일 생성
outputFile << "Hello, Filesystem!" << std::endl;
outputFile.close();
std::cout << "Created file: " << filePath << std::endl;
// 디렉토리 생성
fs::path dirPath = "example_directory";
if (!fs::exists(dirPath)) { // 디렉토리가 존재하지 않는 경우에만 생성
fs::create_directory(dirPath);
std::cout << "Created directory: " << dirPath << std::endl;
}
// 이름 변경
fs::path newFilePath = "new_example.txt";
fs::rename(filePath, newFilePath); // 파일 이름 변경
std::cout << "Renamed file from " << filePath << " to " << newFilePath << std::endl;
// 디렉토리 내용 출력
fs::path currentDir = fs::current_path();
std::cout << "Current directory: " << currentDir << std::endl;
std::cout << "Directory contents:" << std::endl;
for (const auto& entry : fs::directory_iterator(currentDir)) {
std::cout << entry.path() << std::endl;
}
return 0;
}
반응형
'C&C++' 카테고리의 다른 글
| [C++] std::string (0) | 2024.07.07 |
|---|---|
| [C++11] thread (0) | 2024.06.29 |
| [C++11] constexpr (0) | 2024.06.23 |
| [C++11] nullptr (0) | 2024.06.23 |
| [C++11] std::array (0) | 2024.06.23 |