C++C++17C++20Introduction to C++Learn C++

What Is The Filesystem Library In Modern C++ 17

What Is The Filesystem Library In Modern C++ 17

In modern C++, the filesystem library allows portable interaction with directories and directory-like structures providing functions such as listing directory contents and moving files. After the C++17 standard, the contents of the Filesystems Technical Specification are now part of modern C++ and are implemented in the filesystem library.

What Is the filesystems library in C++ 17?

The Filesystem Library is defined in the <filesystem> header (as a std::filesystem namespace), it provides operations on file systems and their components, such as paths, regular files, and directories. This library allows portable interaction with directories and directory-like structures by using classes and non-member functions. It is modernized well for C++, it is largely modeled on POSIX, and flexible enough to be implementable for different operating systems. After the C++17 standard, the contents of the Filesystems Technical Specification are now part of modern C++ and are implemented in the filesystem library.

The filesystem library was previously being used by the boost.filesystem which was published in 2015. In C++17, they merged this library into modern C++. Note that, the boost implementation libraries are still available on more compilers and platforms for many benefits.

The filesystem library consists of a lot of file operations (copy, move, permissions), directory operations (listing, iterating, …), and path operations.

Some of classes are path, directory_entry, directory_iterator, perms, file_status, … and some of non-member functions in this library are copy, copy_file, current_path, exists, file_size, rename, remove, status, is_directory, is_empty, …

Are there some examples of how to use the filesystems library in C++?

Here are some examples that can be used with C++17 and standards beyond it,

How can I use std::filesystem::current_path in C++ 17?

In C++17, we can use std::filesystem::current_path to get current path on runtime. Here is a simple filesystem example in modern C++ that you can get current path.

In my C++ Builder 12 console application example, it gives the current folder of my project that has executable file,

How can I use std::filesystem::copy and std::filesystem::rename in C++ 17?

In C++17, we can use std::filesystem::copy to copy files and directories, and we can use std::filesystem::rename to rename or move files. Here is an example of how you can copy a file and move it by renaming it.

How can I use std::filesystem::directory_iterator in C++ 17?

In C++17, the std::filesystem::directory_iterator is an iterator that iterates over the directory_entry elements of a directory. It doesn’t list sub directories.

Here is an example that lists the contents of a directory (a kind of dir command).

Of course, there are many examples of the classes and methods of the filesystem library. If you want to know more about the filesystem library, in C++ Builder 12, Virtual Assist (VA) is very useful. If you type std::filesystem:: into Visual Assist it automatically lists all members of the filesystem namespace. If you type a method i.e. std::filesystem::copy it gives all variations of its parameters as shown below.

For more details about changes in C++17 standard, please see this https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0218r0.html, or other papers here P0219R1P0317R1P0392R0P0430R2P0492R2LWG 2956.

What Is The Filesystem Library In Modern C++ 17 C++ Builder logo

C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows operating system. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Learn C++Syntax

What Is Set (std::set) In Modern C++?

C++C++11C++14C++17C++20IteratorsLearn C++

What Is Deque (std::deque) In Modern C++?

C++C++17C++20IteratorsLearn C++

What Is forward_list (std::forward_list) In Modern C++?

C++C++17ComponentsGame DevelopmentLanguage FeatureLearn C++

Learn To Create A Torus As A Custom 3D Mesh Object in C++ Builder