C++20 sections · 1024 units
Open in Course

The fstream Header

File stream library

To work with files, include the fstream header. It provides three classes: ifstream for reading, ofstream for writing, and fstream for both. These work like cin and cout but target files instead of the console.

The syntax mirrors iostream: #include at the top of your file. Now you can declare file stream objects like ifstream input("data.txt"); to read from data.txt. This header gives you everything you need for file I/O without external libraries.

The standard library handles buffering, error checking, and resource cleanup automatically when streams go out of scope.