👉🏻 If you want to enjoy the full experience exploring this pdf, check it out here Modern C++ Concepts OOP
introduced in C++17, enhanced in C++20
Methods and classes in filesystem library:
#include <iostream>
#include <filesystem>
using namespace std;
namespace fs = std::filesystem;
int main() {
//the generic path
filesystem::path p1 = "/home/user";
//the specific path
fs::path p2 = "documents/file.txt";
//the / operator is overloaded in the filesystem library to concatenate paths
fs::path full_path = p1 / p2;
cout << "Full Path: " << full_path;
return 0;
}
#include <iostream>
#include <filesystem>
#include <fstream>
namespace fs = std::filesystem;
int main() {
// Create a directory in the current working directory
fs::create_directory("new_directory");
// Create or open a file
//The file is immediately closed after creation
//because no operations are performed on it.
std::ofstream("new_directory/example.txt");
// Remove file
fs::remove("new_directory/example.txt");
// Remove directory
// To remove a directory, it must be empty
//if it contains files or sub directories the operation will fail
fs::remove("new_directory");
return 0;
}
auto[v1, v2, v3, ...] = object;
//the auto keyword automatically deduce the type of the elements
//the object could be a tuple, pair, array, or a custom class
//Unpacking a tuple
#include <tuple>
int main() {
tuple <int, double, char, string> t = {1, 3.14, 'A', "Hello"};
//store the values of the tuple into variables
auto [x, y, z, w] = t;
//instead of looping, you can just cout the values
cout << x << " " << y << " " << z << " " << w;
}
<ranges> header