Working with the File System in C++: A Practical Guide

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

Introduction: Why Working with the File System in C++ is Fundamental

\n\n

Any serious program will sooner or later need to save data to disk or read it from there. Configuration files, logs, databases, user documents — all of this requires the ability to interact with the file system. In C++, there are two main approaches for this: classic input/output streams (the <fstream> library) and the more modern, higher-level std::filesystem (since C++17).

\n\n

In this article, we will cover practical scenarios: from simple reading and writing of text files to navigating directories and error handling. All examples are written in C++17/20 and are ready to use in your projects.

\n\n

1. Basics: Reading and Writing Files with fstream

\n\n

The <fstream> library provides three main classes: ifstream (input stream — reading), ofstream (output stream — writing), and fstream (bidirectional stream). Let's look at them with examples.

\n\n

1.1. Writing to a Text File

\n\n

The simplest way to write data is to use ofstream. If the file does not exist, it will be created. If it exists, it will be overwritten (ios::trunc mode by default).

\n\n
#include <iostream>\n#include <fstream>\n#include <string>\n\nint main() {\n    std::ofstream outFile("example.txt");\n    if (!outFile) {\n        std::cerr << "Error opening file for writing\\n";\n        return 1;\n    }\n\n    outFile << "Hello, C++ file system!\\n";\n    outFile << "Line number 2\\n";\n    outFile << 42 << " — is a number.\\n";\n\n    outFile.close(); // closing manually is optional, but good practice\n    std::cout << "Data written.\\n";\n    return 0;\n}
\n\n

1.2. Reading from a Text File

\n\n

For reading, we use ifstream. The most reliable way is to read line by line using std::getline.

\n\n
#include <iostream>\n#include <fstream>\n#include <string>\n\nint main() {\n    std::ifstream inFile("example.txt");\n    if (!inFile) {\n        std::cerr << "File not found or cannot be opened\\n";\n        return 1;\n    }\n\n    std::string line;\n    while (std::getline(inFile, line)) {\n        std::cout << "Read: " << line << '\\n';\n    }\n\n    inFile.close();\n    return 0;\n}
\n\n

1.3. Open Modes and Binary Files

\n\n

You can combine open flags: ios::app (append), ios::binary (binary mode), ios::ate (seek to end). For binary files, use the read() and write() methods.

\n\n
#include <fstream>\n#include <vector>\n\n// Example: writing and reading a vector of integers in binary format\nvoid saveVector(const std::string& filename, const std::vector<int>& data) {\n    std::ofstream file(filename, std::ios::binary);\n    if (!file) return;\n    file.write(reinterpret_cast<const char*>(data.data()), data.size() * sizeof(int));\n}\n\nstd::vector<int> loadVector(const std::string& filename) {\n    std::ifstream file(filename, std::ios::binary);\n    if (!file) return {};\n    file.seekg(0, std::ios::end);\n    size_t size = file.tellg() / sizeof(int);\n    file.seekg(0, std::ios::beg);\n    std::vector<int> data(size);\n    file.read(reinterpret_cast<char*>(data.data()), size * sizeof(int));\n    return data;\n}
\n\n

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

\n\n

Starting from C++17, the standard library includes the <filesystem> module, which allows you to work with paths, files, and directories at a high level, cross-platform.

\n\n

2.1. Checking Existence and Getting File Information

\n\n

More

Blogs

Book Recommendations