Stay up-to-date with Modern C++

gcc 7 experimental filesystem

Last Update: 10 June 2024

C++17 in Detail: Filesystem in The Standard Library

gcc 7 experimental filesystem

Table of Contents

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!

This article was initially written in 2017, but it’s now updated. Main changes: updated compiler notes and C++20/23 changes, consistent tags, Compiler Explorer examples, layout.

Intro  

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

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

The plan for the series

  • Fixes and deprecation
  • Language clarification
  • Simplification
  • Library changes - Filesystem ( today )
  • Library changes - Parallel Algorithms
  • Library changes - Utils
  • Wrap up, Bonus - with a free ebook! :)

And see more:

  • All Major C++17 Features You Should Know - C++ Stories
  • 17 Smaller but Handy C++17 Features - C++ Stories

Documents & Links  

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++

Also, you can grab my list of concise descriptions of all of the C++17 (plus C++20) - It’s a one-page reference card:

Resources about C++17 STL:

gcc 7 experimental filesystem

  • C++17 In Detail by Bartek!
  • C++17 - The Complete Guide by Nicolai Josuttis
  • C++ Fundamentals Including C++ 17 by Kate Gregory
  • Practical C++14 and C++17 Features - by Giovanni Dicanio
  • C++17 STL Cookbook by Jacek Galowicz

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

Filesystem Overview  

I think the Committee made a right choice with this feature. The filesystem library is nothing new, as it’s modeled directly over Boost filesystem, which is available since 2003 (with the version 1.30). There are only a little 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 I am 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 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
  • and others…

Compiler/Library support  

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

  • GCC: Libstdc++ supports the <filesystem> since GCC 8.1, see Chapter Status
  • Clang 7.0 completed its work on std::filesystem support, see libc++ C++17 Status — libc++ documentation
  • Visual Studio: VS 2017 15.7 supports the whole feature, see Microsoft C/C++ language conformance | Microsoft Learn

Examples  

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

Working with the Path object  

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

For example, let’s examine a path:

Run @Compiler Explorer

Here’s an output for a file path like "C:\Windows\system.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:

The output:

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:

Run code @Compiler Explorer

What can we do more?

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

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

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:

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:

The core part is:

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

Not bad, right?

You can play with the sample here @Compiler Explorer

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:

  • examples like: Working with filesystem paths, Creating, copying, and deleting files and directories, Removing content from a file, Checking the properties of an existing file or directory, searching.
  • examples: path normalizer, Implementing a grep-like text search tool, Implementing an automatic file renamer, Implementing a disk usage counter, statistics about file types,Implementing a tool that reduces folder size by substituting duplicates with symlinks
  • C++17- std::byte and std::filesystem - ModernesCpp.com
  • How similar are Boost filesystem and the standard C++ filesystem libraries? - Stack Overflow
See all articles related to std::filesystem here in the Archive/filesystem tag page.

Summary  

I think the filesystem library is an excellent part of the C++ Standard Library. A lot of time I had to use various API 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% cases.

The feature is based on Boost, so not only a lot of developers are 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 everting can be achieved using std:: prefix and not some strange API :)

I've prepared a valuable bonus if you're interested in Modern C++! Learn all major features of recent C++ Standards! Check it out here:

Similar Articles:

  • C++17 in details: Standard Library Utilities
  • Activity Indicators - Example of a Modern C++ Library
  • How C++17 Benefits from the Boost Libraries
  • Converting from Boost to std::filesystem
  • std::filesystem in C++17 In Detail

IMAGES

  1. Early GCC 7 Compiler Benchmarks On Linux: Some Performance Improvements

    gcc 7 experimental filesystem

  2. C++ : How to determine whether to use filesystem or experimental

    gcc 7 experimental filesystem

  3. GCC 7.3 compiler installation method and C++17 standard test example in

    gcc 7 experimental filesystem

  4. Building GCC 7 on Windows Subsystem for Linux

    gcc 7 experimental filesystem

  5. GCC 7.0 GNU Compiler Collection Internals 1/2

    gcc 7 experimental filesystem

  6. GCC 7.5.0 with --std=c++17: filesystem: No such file or directory

    gcc 7 experimental filesystem

COMMENTS

  1. Link errors using <filesystem> members in C++17

    I'm using gcc 7.2 on Ubuntu 16.04, and I need to use the new filesystem library from C++17. Even though there is indeed a library called …