This chapter dives into the world of file systems and their interaction with C++ programs. We'll explore the built-in
A file system is a structured way of organizing files and directories on a storage device (like a hard drive or SSD). It provides mechanisms for creating, deleting, reading, writing, and managing files and folders.
The <filesystem>
header provides a comprehensive and portable set of classes and functions for file system operations. It offers a safer and more user-friendly alternative to traditional C-style file I/O functions (like fopen
, fread
, etc.).
path
class: Represents a file or directory path in a platform-independent way.<filesystem>
for Basic File I/O
#include
#include
int main() {
// Create a path object
std::filesystem::path my_file = "data.txt";
// Check if the file exists
if (std::filesystem::exists(my_file)) {
std::cout << "File " << my_file << " already exists." << std::endl;
} else {
// Create the file
std::filesystem::create_file(my_file);
std::cout << "File " << my_file << " created successfully." << std::endl;
}
// Open the file for writing (append mode)
std::ofstream output_file(my_file, std::ios::app);
if (output_file.is_open()) {
output_file << "This is some text written to the file." << std::endl;
output_file.close();
} else {
std::cerr << "Error opening file for writing." << std::endl;
}
return 0;
}
<iostream>
for input/output and <filesystem>
for file system operations.path
object representing the file “data.txt”.std::filesystem::exists
.std::filesystem::create_file
.std::ofstream
and check if it’s open successfully.This is a basic example, but it demonstrates the ease of use and safety features of the <filesystem>
header.
C++ provides a set of C-style functions like fopen
, fread
, fwrite
, etc. for file I/O. While still functional, these functions require manual memory management and are prone to errors. We won’t cover them in detail here
Some operating systems offer their own file system libraries with functionalities beyond the standard C++ library. These libraries might provide additional features or optimized performance for specific tasks.
open
, read
, write
) are widely used on Unix-like systems for file I/O, offering a lower-level approach.Several third-party libraries offer advanced file system functionalities like:
<filesystem>
header.The choice of approach depends on your specific needs and priorities:
<filesystem>
header (C++17 and later).File systems allow you to control access permissions (read, write, execute) for users and groups. You can also retrieve various file attributes like size, creation time, and modification time using the <filesystem>
header or platform-specific APIs.
The <filesystem>
header and some platform-specific APIs provide functions for creating, managing, and resolving symbolic and hard links.
<filesystem>
header doesn’t directly manage file streams, but you can use them in conjunction with <fstream>
from <iostream>
for reading and writing text files.<filesystem>
header provides directory iterators for traversing directory structures.By understanding the concepts presented in this chapter, you'll be well-equipped to interact with file systems effectively in your C++ programs. The