Working with the File System in C++: A Complete Guide for Beginners

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with the File System in C++: For Beginners



Working with the file system is one of the key tasks for any programmer. In C++, there are several ways to interact with files and directories: from classic fstream streams to the modern std::filesystem (C++17). In this article, we will cover the basic operations: reading and writing files, checking their existence, creating and deleting folders, and navigating the file system. All code examples are written in C++ and are suitable for beginners.



1. Basics of Working with Files via fstream



The classes ifstream (input from file), ofstream (output to file), and fstream (input/output) are basic tools for working with text and binary files. They are located in the header file <fstream>.



Reading from a File

To read data from a file, create an ifstream object and open the file. Make sure the file exists, otherwise the stream will be in an error state.



#include <iostream>#include <fstream>#include <string>

int main() { std::ifstream file("example.txt"); if (!file.is_open()) { std::cerr << "Error opening file"; return 1; }

std::string line; while (std::getline(file, line)) { std::cout << line << std::endl; }

file.close(); return 0;}


Writing to a File

Use ofstream for writing. If the file does not exist, it will be created. If it exists, the contents will be overwritten (unless the std::ios::app flag is specified).



#include <fstream>

int main() { std::ofstream file("output.txt"); if (!file.is_open()) { std::cerr << "Failed to create file"; return 1; }

file << "Hello, world!"; file << "C++ file system";

file.close(); return 0;}


Appending Data to the End of a File

Use the std::ios::app flag (append) to add data to the end without deleting previous content.



std::ofstream file("log.txt", std::ios::app);file << "New entry";file.close();


2. Working with Directories and the File System (C++17)



Starting from C++17, the standard library includes the header <filesystem>, which provides a convenient and cross-platform interface for working with the file system. To use it, add the compilation flag -std=c++17 (or higher) and include <filesystem>.



Checking the Existence of a File or Directory

#include <iostream>#include <filesystem>namespace fs = std::filesystem;

int main() { fs::path p = "example.txt"; if (fs::exists(p)) { std::cout << "File exists"; std::cout << "Size: " << fs::file_size(p) << " bytes"; } else { std::cout << "File not found"; } return 0;}


Creating and Deleting Directories

#include <filesystem>namespace fs = std::filesystem;

int main() { // Creating a single directory if (fs::create_directory("my_folder")) { std::cout << "Folder created"; }

// Creating nested directories fs::create_directories("a/b/c");

// Deleting an empty directory fs::remove("my_folder");

// Deleting a directory with all its contents (caution!) // fs::remove_all("a");

return 0;}


Iterating Over Directory Contents

#include <iostream>#include <filesystem>namespace fs = std::filesystem;

int main() { std::string path = "."; for (const auto& entry : fs::directory_iterator(path)) { if

Blogs

Book Recommendations