C++ - Filesystem (Deep Dive)
Overview
Estimated time: 60–80 minutes
Use std::filesystem to manipulate paths, iterate directories, copy files, and handle errors portably.
Learning Objectives
- Compose paths and iterate directories safely.
- Copy/move/remove files and handle errors via error_code.
Prerequisites
Examples
#include
#include
int main(){
namespace fs = std::filesystem;
fs::path p = fs::path("data") / "log.txt";
std::cout << p.string() << "\n";
for (auto& e : fs::directory_iterator(".")) std::cout << e.path() << "\n";
}
Common Pitfalls
- Throwing exceptions on filesystem errors by default; prefer overloads with std::error_code for non-throwing behavior.