/* The C++ standard library includes a filesystem library, which is part of the C++17
standard and later. This library provides various classes and functions for working
with file systems and directories, such as creating, copying, and deleting files and
directories, as well as querying information about them. Some examples of functions
in the filesystem library include std::filesystem::create_directory,
std::filesystem::remove, and std::filesystem::exists. To use the filesystem library,
you need to include the <filesystem> header in your C++ program and use the
std::filesystem namespace. */
Here is an example of how to use the C++ filesystem library to create a new
directory:
Copy code
#include <iostream>
#include <filesystem>
int main()
{
std::string new_directory = "test_directory";
std::filesystem::create_directory(new_directory);
std::cout << "Directory " << new_directory << " created." << std::endl;
return 0;
}
This code creates a new directory called "test_directory" in the current working
directory. If the directory already exists, the create_directory function will
throw an error.
Another example is to check whether a file or directory exist or not
Copy code
#include <iostream>
#include <filesystem>
int main()
{
std::string path = "example.txt";
if(std::filesystem::exists(path))
std::cout << path << " exists" << std::endl;
else
std::cout << path << " does not exist" << std::endl;
return 0;
}
This code checks whether a file called "example.txt" exists in the current working
directory. It will output "example.txt exists" if the file exists and
"example.txt does not exist" if it doesn't.
Please note that the std::filesystem namespace is introduced in C++17, so you
will need a compiler that supports C++17 or later to use it.
To embed this project on your website, copy the following code and paste it into your website's HTML: