C++17 in Details: Filesystem

DZone 's Guide to

C++17 in Details: Filesystem

Interested in learning the nitty gritty about the latest iteration of C++? Read on to get one developers overview and some sample code.

· Web Dev Zone ·
Free Resource

Although C++ is an old programming language, its Standard Library misses a few basic things. Features that Java or .NET had for years were/are not available in STL. With C++17, there's a nice improvement: for example, we now have the standard filesystem!

Traversing a path, even recursively is so simple now!


For the last five episodes/articles, I've covered most of the language features. Now it's time for the Standard Library. I've planned three posts on that: Filesystem, Parallel STL and Concurrency, and Utils.

Maybe I was a bit harsh in the introduction. Although the Standard Library lacks some important features, you could always use Boost with its thousands of sub-libraries and do the work. The C++ Committee and the Community decided that the Boost libraries are so important that some of the systems were merged into the Standard. For example, smart pointers (although improved with the move semantics in C++11), regular expressions, and much more.

A similar story happened with the filesystem. Let's try to understand what's inside.

The Series

This post is the sixth in the series about C++17 features details.

First of all, if you want to dig into the standard on your own, you can read the latest draft here:

N4659, 2017-03-21, Working Draft, Standard for Programming Language C++ - the link also appears on the isocpp.org.

OK, let's return to our main topic: working with paths and directories!

Filesystem Overview

I think the Committee made the right choice with this feature. The filesystem library is nothing new, as it's modeled directly over Boost filesystem, which has been available since 2003 (with the version 1.30). There are only a few differences, plus some wording changes. Not to mention, all of this is also based on POSIX.

Thanks to this approach it's easy to port the code. Moreover, there's a good chance a lot of developers are already familiar with the library. (Hmmm... so why am I not that dev?)

The library is located in the <filesystem> header. It uses namespace std::filesystem.

The final paper is P0218R0: Adopt the File System TS for C++17 but there are also others like P0317R1: Directory Entry Caching, PDF: P0430R2-File system library on non-POSIX-like operating systems, P0492R2 ... All in all, you can find the final spec in the C++17 draft: the "filesystem" section, 30.10.

We have three/four core parts:

  • The path object
  • directory_entry
  • Directory iterators
  • Plus many supportive functions:
    • getting information about the path
    • files manipulation: copy, move, create, symlinks
    • last write time
    • permissions
    • space/filesize

Compiler/Library support

Depending on the version of your compiler you might need to use the std::experimental::filesystem namespace.

  • GCC: You have to specify -lstdc++fs when you want a filesystem. Implemented in <experimental/filesystem>.
  • Clang should be ready with Clang 5.0
  • Visual Studio: In VS 2017 (2017.2) you still have to use std::experimental namespace, it uses TS implementation.


All the examples can be found on my Github: github.com/fenbf/articles/cpp17.

I've used Visual Studio 2017 Update 2.

Working With the Path Object

The core part of the library is the path object. Just pass it as a string of the path, and then you have access to lots of useful functions.

For example, let's examine a path:

namespace fs = std::experimental::filesystem;

fs::path pathToShow(/* ... */);
cout << "exists() = " << fs::exists(pathToShow) << "\n"
     << "root_name() = " << pathToShow.root_name() << "\n"
     << "root_path() = " << pathToShow.root_path() << "\n"
     << "relative_path() = " << pathToShow.relative_path() << "\n"
     << "parent_path() = " << pathToShow.parent_path() << "\n"
     << "filename() = " << pathToShow.filename() << "\n"
     << "stem() = " << pathToShow.stem() << "\n"
     << "extension() = " << pathToShow.extension() << "\n";

Here's an output for a file path like "C:\Windows\system.ini":

exists() = 1
root_name() = C:
root_path() = C:\
relative_path() = Windows\system.ini
parent_path() = C:\Windows
filename() = system.ini
stem() = system
extension() = .ini

What's great about the above code?

It's so simple to use! But there's more cool stuff.

For example, if you want to iterate over all the elements of the path just write:

int i = 0;    
for (const auto& part : pathToShow)
    cout << "path part: " << i++ << " = " << part << "\n";

The output:

path part: 0 = C:
path part: 1 = \
path part: 2 = Windows
path part: 3 = system.ini

We have several things here:

  • The path object is implicitly convertible to std::wstring or std::string. So you can just pass a path object into any of the file stream functions.
  • You can initialize it from a string, const char*, etc. Also, there's support for string_view, so if you have that object around there's no need to convert it to string before passing to path. PDF: WG21 P0392
  • path has begin() and end() (so it's a kind of a collection!) that allows iterating over every part.

What about composing a path?

We have two options: using append or operator /=, or operator +=.

  • append - adds a path with a directory separator.
  • concat - only adds the 'string' without any separator.

For example:

fs::path p1("C:\\temp");
p1 /= "user";
p1 /= "data";
cout << p1 << "\n";

fs::path p2("C:\\temp\\");
p2 += "user";
p2 += "data";
cout << p2 << "\n";



What more can we do?

Let's find a file size (using file_size ):

uintmax_t ComputeFileSize(const fs::path& pathToCheck)
    if (fs::exists(pathToCheck) &&
        auto err = std::error_code{};
        auto filesize = fs::file_size(pathToCheck, err);
        if (filesize != static_cast<uintmax_t>(-1))
            return filesize;

    return static_cast<uintmax_t>(-1);

Or, how to find the last modified time for a file:

auto timeEntry = fs::last_write_time(entry);
time_t cftime = chrono::system_clock::to_time_t(timeEntry);
cout << std::asctime(std::localtime(&cftime));

Isn't that nice?

As an additional information, most of the functions that work on a path have two versions:

  • One that throws: filesystem_error
  • Another with error_code (system specific)

Let's now take a bit more advanced example: how to traverse the directory tree and show its contents.

Traversing a path

We can traverse a path using two available iterators:

  • directory_iterator
  • recursive_directory_iterator - iterates recursively, but the order of the visited files/dirs is unspecified, each directory entry is visited only once.

In both iterators, the directories . and .. are skipped.

Ok... show me the code:

void DisplayDirTree(const fs::path& pathToShow, int level)
    if (fs::exists(pathToShow) && fs::is_directory(pathToShow))
        auto lead = std::string(level * 3, ' ');
        for (const auto& entry : fs::directory_iterator(pathToShow))
            auto filename = entry.path().filename();
            if (fs::is_directory(entry.status()))
                cout << lead << "[+] " << filename << "\n";
                DisplayDirTree(entry, level + 1);
                cout << "\n";
            else if (fs::is_regular_file(entry.status()))
                DisplayFileInfo(entry, lead, filename);
                cout << lead << " [?]" << filename << "\n";

The above example uses not a recursive iterator but does the recursion on its own. This is because I'd like to present the files in a nice, tree style order.

We can also start with the root call:

void DisplayDirectoryTree(const fs::path& pathToShow)
    DisplayDirectoryTree(pathToShow, 0);

The core part is:

for (auto const & entry : fs::directory_iterator(pathToShow))

The code iterates over entries, each entry contains a path object plus some additional data used during the iteration.

Not bad, right?

Of course, there's more stuff you can do with the library:

  • Create files, move, copy, etc.
  • Work on symbolic links, hard links.
  • Check and set file flags.
  • Count disk space usage, stats.

Today I wanted to give you a general glimpse over the library. As you can see, there are more potential topics for the future.

More Resources

You might want to read:


I think the filesystem library is an excellent part of the C++ Standard Library. A lot of time I had to use various APIs to do the same tasks on different platforms. Now, I'll be able to just use one API that will work for, probably, 99.9% of the cases.

The feature is based on Boost, so not only are a lot of developers familiar with the code/concepts but also it's proven to work in many existing projects.

And look at my samples: isn't traversing a directory and working with paths so simple now? I am happy to see that everything can be achieved using std:: prefix and not some strange API. 

c++, c++17, web application development, web dev

Published at DZone with permission of Bartłomiej Filipek , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}