Site icon 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.

[crayon-67639b23ab7b1032850502/]

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

[crayon-67639b23ab7bc748960193/]

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.

[crayon-67639b23ab7c0156532723/]

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

[crayon-67639b23ab7c2791291378/]

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.

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.

Exit mobile version