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.
Table of Contents
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> #include <filesystem> int main() { std::cout << "Current path is " << std::filesystem::current_path() << std::endl; system("pause"); return 0; } |
In my C++ Builder 12 console application example, it gives the current folder of my project that has executable file,
1 2 3 |
Current path is C:\Users\yilmaz\Documents\Embarcadero\Studio\Projects\Win32\Debug |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> #include <filesystem> int main() { std::filesystem::copy("C:\\test.txt", "D:\\test.txt"); std::filesystem::rename("D:\\test.txt", "D:\\mytest.txt"); system("pause"); return 0; } |
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).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <filesystem> int main() { std::string mypath = "C://"; for (auto& line : std::filesystem::directory_iterator(mypath) ) { std::cout << line.path() << std::endl; } system("pause"); return 0; } |
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 P0219R1, P0317R1, P0392R0, P0430R2, P0492R2, LWG 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.