File System Library in C++17
Last Updated :
05 Oct, 2023
In this article, we will learn about the File System library in C++17 and with examples. <filesystem> header was added in C++17 and introduces a set of classes, functions, and types that simplify file system operations. In simple words, we can say that the filesystem library provides tools that help us to simplify working with files and directories.
In earlier versions, performing file and directory operations was often a bulky and mistake-susceptible task as it required the use of platform-specific functions and libraries. The file system library was added to cope with these troubles, offering a portable and standardized way to paint with the file system.
Syntax
To use the features of the file system library, we have to import <filesystem> header using #include preprocessor.
#include <filesystem>
All the identifiers of <filesystem> headers are defined inside the std::filesystem namespace.
Classes in <filesystem> Header
The following are some commonly used classes of file system libraries.
S. No.
| Class
| Description
|
---|
1
| filesystem::path | The path class represents the path of the directory or the file. |
---|
2
| filesystem::copy_options | This class represents the available copy options. |
---|
3
| filesystem::directory_options | Represents the options for iterating through directory contents. |
---|
4
| filesystem::filesystem_error | This class defines the exceptions that are thrown by the filesystem functions in case of errors. |
---|
5
| filesystem::file_status | This class stores information about the type of the file and available permissions. |
---|
6
| filesystem::file_time_type | It represents file time values |
---|
7
| filesystem::perms | This function stores the information about the file access permission. |
---|
8
| filesystem::perm_options | It specifies the semantics of permissions operations. |
---|
9
| filesystem::space_info | This struct stores the data about the storage in the specified path. |
---|
10
| filesystem::file_type: | It is used to represent different types of a file or directory. |
---|
Examples of File System library
Example 1: Creating a Directory and File
In this example, we will create a new file in a newly created directory. The parent directory looks like this before execution:
Parent Directory Before Execution
C++
// C++ Program to illustrate the use of filesystem classes
// and features
#include <filesystem>
#include <fstream>
#include <iostream>
// for simplicity
using namespace std;
using namespace std::filesystem;
int main()
{
// Define the path to create directory
path directorypath = "mydirectory";
// To check if the directory exist or not, create it if
// doesn't exist
if (!exists(directorypath)) {
create_directory(directorypath);
cout << "Directory created: " << directorypath
<< endl;
}
// Define the file path within the directory and
// combining the directory
path filepath = directorypath / "my_file.txt";
// Create and open the file for writing using
// std::ofstream
ofstream file(filepath);
if (file.is_open()) {
// Write data to the file
file << "Hello, FileSystem!";
file.close();
cout << "File created: " << filepath << endl;
}
else {
// Handle the case if any error occured
cerr << "Failed to create file: " << filepath
<< endl;
}
return 0;
}
Output
Directory created: "mydirectory"
File created: "mydirectory/my_file.txt"
Explanation
In this example, a directory is created named "mydirectory". It checks if the directory exists and creates it if no longer. Sooner or later, a file named "my_file.Txt" is defined within this directory. The code then opens this file for writing using std::ofstream. If a success, it writes the text "Hello, FileSystem!" to the file and closes it. If any errors occur in the directory of the listing or file advent procedure, suitable error messages are displayed.
Parent Directory After Execution
New Directory
Contents of New FileExample 2: Listing Files in a Directory
C++
// C++ program to list all the files and folder in the given
// directory
#include <filesystem>
#include <iostream>
using namespace std;
using namespace std::filesystem;
int main()
{
// Define the directory path to list files from
path directorypath = "";
// To check if the directory exists or not
if (exists(directorypath)
&& is_directory(directorypath)) {
// Loop through each item (file or subdirectory) in
// the directory
for (const auto& entry :
directory_iterator(directorypath)) {
// Output the path of the file or subdirectory
cout << "File: " << entry.path() << endl;
}
}
else {
// Handle the case where the directory doesn't exist
cerr << "Directory not found." << endl;
}
return 0;
}
Output
File: "mydirectory/my_file.txt"
File: "mydirectory/my_file2.txt"
File: "mydirectory/my_file3.txt"
File: "mydirectory/my_file4.txt"
Explanation
In this example, first, we define the directorypath to indicate the target directory. Then we wrote the condition to check if the directory exists or not using fs::exists() and fs::is_directory(). If it exists, it iterates through its contents using a range-based for loop with fs::directory_iterator(), and prints each item's path to the standard output.
Example 3: Renaming a File
C++
// C++ program to rename a file
#include <filesystem>
#include <iostream>
using namespace std;
using namespace std::filesystem;
int main()
{
// Define the path of old file and new file
path oldFilePath = "mydirectory/my_file.txt";
path newFilePath = "mydirectory/renamed_file.txt";
// Check if the old file exists
if (exists(oldFilePath)) {
// Rename the file
rename(oldFilePath, newFilePath);
cout << "File renamed to: " << newFilePath << endl;
}
else {
// Handle the case where the old file doesn't exist
cerr << "File not found: " << oldFilePath << endl;
}
return 0;
}
Output
File renamed to: "mydirectory/renamed_file.txt"
Explanation
In this example, first, we define the path for the old file and the new file. Then, wrote a condition to check if the old file exists in the directory or not with fs::exists(), and if it is discovered, then rename it using fs::rename(). A success message will be displayed with the new file path. and if the old file isn't found, it prints an error message.
Advantages and Features of <filesystem>
Let's explore some of the key features provided by the <filesystem> library:
- Path Manipulation: The file system library introduces the std::filesystem::path class to represent file system paths. This class encapsulates the platform-specific path representation and provides an easy way to manipulate and inspect paths.
- File and Directory Operations: The file system library includes functions to perform common file and directory operations such as creating, removing, renaming, and checking for the existence of files and directories.
- Error Handling: The <filesystem> library provides exceptions to handle errors during file system operations. You can catch exceptions like std::filesystem::filesystem_error to gracefully handle failures.
- Portable Code: One of the main advantages of using <filesystem> is the portability it brings to your code. Since it abstracts platform-specific details, you can write code that works consistently across different operating systems.
Similar Reads
Rust - Creating a Library
Rust is a multi-paradigm programming language like C++ syntax that was designed for performance and safety, especially for safe concurrency. Also, it is a compiled system programming language. In this article, we will see how to create libraries in Rust. Creating a Rust Library:Step 1: We start by c
1 min read
<strings> library in C++ STL
Member functions String.constructor : Construct string object (public member function ).String.destructor : String destructor (public member function )String.operator= : String assignment (public member function ) Iterators Begin : Return iterator to beginning (public member function )End : Return i
3 min read
<regex> library in C++ STL
Main classes These classes encapsulate a regular expression and the results of matching a regular expression within a target sequence of characters. basic_regex: Regular expression object (class template) sub_match: Identifies the sequence of characters matched by a sub-expression (class template) m
2 min read
C++23 Library - <spanstream> Header
The <spanstream> header is a new addition to C++ 23 Standard Libraries Collection. It provides fixed character buffer streams for input and output. It is a collection of classes and function templates that let you manipulate letter stretch as if they were streams, much like <stringstream
3 min read
<numeric> library in C++ STL
Common mathematical functions std::fabs: This function returns the absolute value. std::sqrt: This function returns the square root std::sin: This function returns the sine measured in radians. Special mathematical functions std::beta: This function evaluates the (complete) Beta integral with given
2 min read
RapidJSON - File Read/Write in C++
RapidJSON is a C++ library for parsing and generating JSON (JavaScript Object Notation) data. Â It is designed for high performance and can handle very large JSON documents. RapidJSON supports both reading and writing JSON data, as well as validation and manipulation of JSON objects. It also includes
6 min read
Inline Variables in C++ 17
An inline variable in C++ is a variable that is declared using an inline specifier. It is an exception to one definition having multiple definitions across various translation units. Inline variables have an external linkage when not declared as static. Syntaxinline data_type variable_name = initial
3 min read
Copy File To Vector in C++ STL
Prerequisite:Â Vectors in C++ STLFile Handling in C++ The C++ Standard Template Library (STL) provides several useful container classes that can be used to store and manipulate data. One of the most commonly used container classes is the vector. In this article, we will discuss how to copy the conte
2 min read
How to Create a Dynamic Library in C++?
In C++, dynamic libraries also known as shared libraries are a powerful way to modularize your code. They allow the users to build libraries that can be loaded at runtime by multiple applications at once. This approach promotes code reuse, reduces code duplication, and simplifies maintenance of the
4 min read
C++ Libraries for Machine Learning
Machine learning (ML) has significantly transformed various industries by enabling systems to learn from data and make predictions. While Python is often the go-to language for ML due to its extensive libraries and ease of use, C++ is increasingly gaining attention for ML applications. C++ offers su
5 min read