Boost.Filesystem for C++: Complete Guide to Working with the File System
\n\nBoost.Filesystem is a powerful and mature C++ library that provides a portable (cross-platform) interface for performing file system operations. It is part of the Boost libraries collection and, in fact, became the prototype for the standard <filesystem> library in C++17. If you are working with C++11 or C++14 and need a reliable, time-tested tool for working with paths, files, and directories, Boost.Filesystem is your choice.
Why is it needed? Standard C++ for a long time lacked built-in tools for simple tasks such as checking if a file exists, creating a directory, or recursively traversing folders. Developers had to use stat() on Linux or GetFileAttributes() on Windows, which made code non-portable. Boost.Filesystem solves this problem by providing a single API that works the same on all major platforms. The library encapsulates system calls, handles path encodings (e.g., UTF-8 on Linux and UTF-16 on Windows), and offers convenient classes for working with paths and iterating through directories.
In this article, we will cover in detail the library installation, main classes and methods, and provide a real practical example that you can use in your projects. We will focus on Boost version 1.84 (current at the time of writing), but the examples will work with older versions as well.
\n\nInstalling Boost.Filesystem
\n\nUnlike languages with package managers (npm, pip), C++ requires compiling libraries. Boost.Filesystem is a header-only library only for some parts; for working with files, a compiled library (libboost_filesystem) is required.
Installation via Package Manager (Recommended)
\n\nUbuntu/Debian (Linux):
\nsudo apt update\nsudo apt install libboost-filesystem-dev\n\nmacOS (Homebrew):
\nbrew install boost\n\nWindows (vcpkg):
\nvcpkg install boost-filesystem\n\nBuilding from Source (if no package manager is available)
\n# Download Boost from the official website\nwget https://boostorg.jfrog.io/artifactory/main/release/1.84.0/source/boost_1_84_0.tar.gz\ntar -xzf boost_1_84_0.tar.gz\ncd boost_1_84_0\n\n# Run bootstrap\n./bootstrap.sh\n\n# Build only the filesystem library (saves time)\n./b2 --with-filesystem\n\n# Install (usually to /usr/local)\nsudo ./b2 install\n\nCompiling a Project with Boost.Filesystem
\nWhen compiling, you need to specify the path to the header files and link against the library:
\ng++ -std=c++11 -I/path/to/boost main.cpp -lboost_filesystem -lboost_system -o my_program\n\nQuick Start: Minimal Working Example
\n\nLet's write a simple program that checks if a file exists and prints its size.
\n\n#include <iostream>\n#include <boost/filesystem.hpp>\n\nnamespace fs = boost::filesystem;\n\nint main() {\n // Path to the file\n fs::path file_path("example.txt");\n\n // Check if the file exists\n if (fs::exists(file_path)) {\n // Get the file size\n uintmax_t file_size = fs::file_size(file_path);\n std::cout << "File: " << file_path.filename() << std::endl;\n std::cout << "Size: " << file_size << " bytes" << std::endl;\n } else {\n std::cout << "File not found." << std::endl;\n }\n\n return 0;\n}\n\nThis code demonstrates three key features: creating a path object (fs::path), checking
Also in library
ImageSharp for C# (.NET): Complete Guide with Examples
Rust PDF Guide: Complete Tutorial with Code Examples