
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Library - <filesystem>
The <filesystem> header in C++, provides a set of functions for performing operations on the file system, such as manipulating paths, copying or deleting files and checking file status.
The <filesystem> library may be unavailable if a hierarchical file system is not accessible to the implementation, or if it does not provide the necessary capabilities. Some features may not be available if they are not supported by the underlying file system, such as FAT(File Allocation Table).
Including <filesystem> Header
To include the <filesystem> header in your C++ program, you can use the following syntax.
#include <filesystem>
Functions of <filesystem> Header
Below is list of all functions from <filesystem> header.
Path Manipulation Functions
This path manipulation functions operate on file paths to compose, convert or resolve them.
S.No | Functions & Description |
---|---|
1 |
absolute
This function composes an absolute path based on a given relative or absolute path. |
2 |
canonical & weakly_canonical
These functions convert a path to its canonical form by by resolving all symbolic links and relative paths. |
3 |
relative, proximate
These functions composes a relative path from one path to another. |
4 |
copy
This function copies files or directories. |
5 |
copy_file
This function copies the contents of a single file. |
6 |
copy_symlink
This function copies a symbolic link. |
7 |
create_directory, create_directories
These functions creates single/multiple new directory. |
8 |
create_hard_link
This function creates a hard link, allowing a single file to have multiple directory entries. |
9 |
create_symlink, create_directory_symlink
These functions creates a symbolic link for a file. |
10 |
current_path
This function returns or sets the current working directory. |
11 |
exists
This function checks whether path refers to existing file system object. |
12 |
equivalent
This function checks whether two paths refer to the same file system object. |
13 |
file_size
This function returns the size of a file. |
14 |
hard_link_count
This function returns the number of hard links referring to the specific file. |
15 |
last_write_time
This function gets or sets the time of the last data modification. |
16 |
permissions
This function modifies file access permissions. |
17 |
read_symlink
This function obtains the target of a symbolic link. |
18 |
remove
This function removes a file or empty directory. |
19 |
remove_all
This function removes a file or removes a file or directory and all its contents, recursively. |
20 |
rename
This function moves or renames a file or directory. |
21 |
resize_file
This function changes the size of a regular file by truncation or zero-fill. |
22 |
space
This function determines available free space on the file system. |
Path Manipulation Example
In the following example we are going to use, exists() checking whether the specified path (filePath) refers to an existing file system object.
#include <iostream> #include <filesystem> int main() { std::filesystem::path filePath{"example.txt"}; if (std::filesystem::exists(filePath)) { std::cout << "The file exists." << std::endl; } else { std::cout << "The file does not exist." << std::endl; } return 0; }
Output
If we run the above code it will generate the following output
The file does not exist.
File types
The <filesystem> library provides several functions that determine the type of file or directory specified by a path. These functions are used for understanding the characteristics of files and implementing logic based on file types.
S.No | Functions & Description |
---|---|
1 |
is_block_file
This function checks whether the given path refers to block device. |
2 |
is_character_file
This function checks whether the given path refers to a character device. |
3 |
is_directory
This function checks whether the given path refers to a directory. |
4 |
is_empty
This function checks whether the given path refers to an empty file or directory. |
5 |
is_fifo
This function checks whether the given path refers to a named pipe. |
6 |
is_other
This function checks whether the argument refers to an other file. |
7 |
is_regular_file
This function checks whether the argument refers to a regular file. |
8 |
is_socket
This function checks whether the argument refers to a named IPC socket. |
9 |
is_symlink
This function checks whether the argument refers to a symbolic link. |
10 |
status_known
This function checks whether file status is known. |
Checking the Specified Path
In the following example we are going to use, is_directory to check if the specified path (dirPath) refers to a directory.
#include <iostream> #include <filesystem> int main() { std::filesystem::path dirPath{"my_directory"}; if (std::filesystem::is_directory(dirPath)) { std::cout << "This path refers to a directory." << std::endl; } else { std::cout << "This path does not refer to a directory." << std::endl; } return 0; }
Output
If we run the above code it will generate the following output
This path does not refer to a directory.