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++: From Basics to Advanced Techniques



Working with the file system is one of the key tasks in any serious C++ application. Whether it's reading configuration files, writing logs, processing large volumes of data, or project management — the ability to correctly interact with files and directories is necessary for every C++ developer. In this article, we will break down the main approaches to working with the file system in C++, starting from classic C functions and ending with the modern std::filesystem, introduced in C++17.



We will look at practical examples that will help you not only understand the theory but also immediately apply the acquired knowledge in real projects. All code examples are tested and ready for use.



1. Basics of Working with Files: Input/Output Streams



The most common way to work with files in C++ is using the <fstream> library. It provides three main classes:



  • std::ifstream — for reading from a file (input file stream);
  • std::ofstream — for writing to a file (output file stream);
  • std::fstream — for reading and writing (file stream).


Reading from a File



To read data from a file, create an std::ifstream object and open the file. The simplest way to read a file line by line is to use the std::getline function:



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

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

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

inputFile.close(); return 0;}


Note the is_open() check — this is an important practice that prevents the program from crashing if the file is missing.



Writing to a File



For writing data, std::ofstream is used. By default, the file is created anew (trunc mode). If you need to append data to the end, use the std::ios::app flag:



#include <iostream>#include <fstream>

int main() { std::ofstream outputFile("output.txt", std::ios::app); if (!outputFile.is_open()) { std::cerr << "Error creating file!" << std::endl; return 1; }

outputFile << "Line 1" << std::endl; outputFile << "Line 2" << std::endl; outputFile << "Number: " << 42 << std::endl;

outputFile.close(); return 0;}


Working with Binary Files



For binary data, use the read() and write() methods specifying the block size:



#include <iostream>#include <fstream>#include <cstring>

struct Record { int id; char name[50]; double value;};

int main() { // Writing Record r = {1, "Test Record", 3.14}; std::ofstream binOut("data.bin", std::ios::binary); binOut.write(reinterpret_cast<const char*>(&r), sizeof(r)); binOut.close();

// Reading Record r2; std::ifstream binIn("data.bin", std::ios::binary); binIn.read(reinterpret_cast<char*>(&r2), sizeof(r2)); binIn.close();

std::cout << "ID: " << r2.id << ", Name: " << r2.name << ", Value: " << r2.value << std::endl; return 0;}


2. Modern Approach: The std::filesystem Library (C++17)



Starting from C++17, the <filesystem> library was added to the standard library, which significantly simplifies working with the file system. O

Blogs

Book Recommendations