File I/O
std::filesystem (C++17)
Manage directories, inspect files, and execute path operations cross-platform using std::filesystem.
Interview: Working with paths, directory iteration, cross-platform normalization, and handling filesystem exceptions safely.
Introduced in C++17, the std::filesystem library provides a standardized, cross-platform way to perform directory traversals, check file properties, and manipulate paths without relying on system-specific APIs.
std::filesystem::path
Represents a path on the file system. It automatically handles platform-specific directory separators (e.g. / vs \\).
Directory Iteration
Traverse directories recursively using recursive_directory_iterator or non-recursively with directory_iterator.
Error Overloads
Most functions have overloads that accept a std::error_code to prevent throwing exceptions if operations fail.
Path Manipulation and Safety
The std::filesystem::path class overloads the / operator, making path concatenation clean and readable:
std::filesystem::path p = "/usr"; p /= "bin"; // Results in "/usr/bin"
To prevent applications from crashing when files are deleted or directories are locked, pass a std::error_code argument to verify the status of filesystem operations.
Code Walkthrough
Iterating through a directory, filtering for regular files, and extracting path metadata.
#include <iostream> #include <filesystem>namespace fs = std::filesystem;
void inspectDirectory(const std::string& dirPath) { fs::path targetDir(dirPath);
std::error_code ec; if (!fs::exists(targetDir, ec)) { std::cerr << "Directory does not exist: " << ec.message() << std::endl; return; }
// Traverse directory contents for (const auto& entry : fs::directory_iterator(targetDir)) { if (entry.is_regular_file()) { std::cout << "File: " << entry.path().filename() << " | Size: " << entry.file_size() << " bytes\n"; } else if (entry.is_directory()) { std::cout << "Subdir: " << entry.path().filename() << "\n"; } } }
int main() { inspectDirectory("."); return 0; }
Interview-Relevant Information
Q: How do you handle filesystem errors in std::filesystem without catch blocks?
Answer: Most functions support an overload that accepts a std::error_code reference (e.g. fs::exists(p, ec)). If an error occurs, the function sets this code instead of throwing a filesystem_error exception, allowing you to handle the error using standard conditional checks.
Q: What is the benefit of using the / operator with std::filesystem::path?
Answer: The / operator performs path concatenation, automatically inserting the correct directory separator for the target operating system (e.g., backslashes on Windows, forward slashes on POSIX). This makes path concatenation cross-platform.
Quick Checklist
Did you use std::error_code for error checking? Do you use the / operator for path concatenation? If yes, your path logic is secure and portable.
Use Cases
Traversing file systems to scan, backup, or clean temp assets in desktop utilities.
Sanitizing user import paths cross-platform in configuration loaders.
Common Mistakes
Assuming directory traversals are sorted alphabetically (the order depends on the OS filesystem structure).
Calling properties like file_size() on paths without verifying they exist first, leading to crashes.