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.NoFunctions & Description
1absolute

This function composes an absolute path based on a given relative or absolute path.

2canonical & weakly_canonical

These functions convert a path to its canonical form by by resolving all symbolic links and relative paths.

3relative, proximate

These functions composes a relative path from one path to another.

4copy

This function copies files or directories.

5copy_file

This function copies the contents of a single file.

6copy_symlink

This function copies a symbolic link.

7create_directory, create_directories

These functions creates single/multiple new directory.

8create_hard_link

This function creates a hard link, allowing a single file to have multiple directory entries.

9create_symlink, create_directory_symlink

These functions creates a symbolic link for a file.

10current_path

This function returns or sets the current working directory.

11exists

This function checks whether path refers to existing file system object.

12equivalent

This function checks whether two paths refer to the same file system object.

13file_size

This function returns the size of a file.

14hard_link_count

This function returns the number of hard links referring to the specific file.

15last_write_time

This function gets or sets the time of the last data modification.

16permissions

This function modifies file access permissions.

17read_symlink

This function obtains the target of a symbolic link.

18remove

This function removes a file or empty directory.

19remove_all

This function removes a file or removes a file or directory and all its contents, recursively.

20rename

This function moves or renames a file or directory.

21resize_file

This function changes the size of a regular file by truncation or zero-fill.

22space

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.NoFunctions & Description
1is_block_file

This function checks whether the given path refers to block device.

2is_character_file

This function checks whether the given path refers to a character device.

3is_directory

This function checks whether the given path refers to a directory.

4is_empty

This function checks whether the given path refers to an empty file or directory.

5is_fifo

This function checks whether the given path refers to a named pipe.

6is_other

This function checks whether the argument refers to an other file.

7is_regular_file

This function checks whether the argument refers to a regular file.

8is_socket

This function checks whether the argument refers to a named IPC socket.

9is_symlink

This function checks whether the argument refers to a symbolic link.

10status_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.