Boost.Filesystem for C++: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Boost.Filesystem for C++: Complete Guide to Working with the File System

\n\n

Boost.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.

\n\n

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.

\n\n

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\n

Installing Boost.Filesystem

\n\n

Unlike 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.

\n\n

Installation via Package Manager (Recommended)

\n\n

Ubuntu/Debian (Linux):

\n
sudo apt update\nsudo apt install libboost-filesystem-dev
\n\n

macOS (Homebrew):

\n
brew install boost
\n\n

Windows (vcpkg):

\n
vcpkg install boost-filesystem
\n\n

Building 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\n

Compiling a Project with Boost.Filesystem

\n

When compiling, you need to specify the path to the header files and link against the library:

\n
g++ -std=c++11 -I/path/to/boost main.cpp -lboost_filesystem -lboost_system -o my_program
\n\n

Quick Start: Minimal Working Example

\n\n

Let'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\n

This code demonstrates three key features: creating a path object (fs::path), checking

Recommendations