How to Use std::filesystem in C++: Syntax and Examples
To use
std::filesystem in C++, include the header <filesystem> and use the namespace std::filesystem. It provides functions to work with files and directories like checking existence, creating directories, and iterating files.Syntax
Include the <filesystem> header and use the std::filesystem namespace. Common classes and functions include:
std::filesystem::path: represents file or directory paths.exists(path): checks if a file or directory exists.create_directory(path): creates a new directory.remove(path): deletes a file or directory.directory_iterator(path): iterates over directory contents.
cpp
#include <filesystem> #include <iostream> namespace fs = std::filesystem; int main() { fs::path p = "example.txt"; // path object if (fs::exists(p)) { std::cout << p << " exists.\n"; } else { std::cout << p << " does not exist.\n"; } return 0; }
Example
This example shows how to create a directory, check if it exists, list files inside it, and remove the directory.
cpp
#include <filesystem> #include <iostream> namespace fs = std::filesystem; int main() { fs::path dir = "test_dir"; // Create directory if it doesn't exist if (!fs::exists(dir)) { fs::create_directory(dir); std::cout << "Directory created: " << dir << "\n"; } else { std::cout << "Directory already exists: " << dir << "\n"; } // List contents (should be empty) std::cout << "Contents of " << dir << ":\n"; for (const auto& entry : fs::directory_iterator(dir)) { std::cout << " " << entry.path().filename() << "\n"; } // Remove directory fs::remove(dir); std::cout << "Directory removed: " << dir << "\n"; return 0; }
Output
Directory created: test_dir
Contents of test_dir:
Directory removed: test_dir
Common Pitfalls
Common mistakes when using std::filesystem include:
- Not checking if a path exists before accessing it, causing exceptions.
- Confusing relative and absolute paths.
- Trying to remove non-empty directories with
remove()instead ofremove_all(). - Forgetting to handle exceptions thrown by filesystem operations.
Always use try-catch blocks or check conditions before operations.
cpp
#include <filesystem> #include <iostream> namespace fs = std::filesystem; int main() { fs::path dir = "nonexistent_dir"; // Wrong: assuming directory exists // This will throw if directory doesn't exist // for (const auto& entry : fs::directory_iterator(dir)) { // std::cout << entry.path() << "\n"; // } // Right: check existence first if (fs::exists(dir)) { for (const auto& entry : fs::directory_iterator(dir)) { std::cout << entry.path() << "\n"; } } else { std::cout << dir << " does not exist.\n"; } return 0; }
Output
nonexistent_dir does not exist.
Quick Reference
| Function | Description |
|---|---|
| fs::exists(path) | Check if a file or directory exists |
| fs::create_directory(path) | Create a new directory |
| fs::remove(path) | Remove a file or empty directory |
| fs::remove_all(path) | Remove a directory and all contents |
| fs::directory_iterator(path) | Iterate over directory contents |
| fs::path | Class to represent file or directory paths |
Key Takeaways
Include and use namespace std::filesystem to access filesystem features.
Always check if paths exist before performing operations to avoid exceptions.
Use fs::create_directory to make directories and fs::remove or fs::remove_all to delete files or directories.
Use directory_iterator to loop through files in a directory.
Handle exceptions or errors when working with filesystem operations.