Deep-dive into File Handling in C++ — covering everything from syntax to idioms, best practices, and key functions. This will be both foundational and idiomatic, aligned with writing clean, modern C++.
#include <fstream>
Gives access to:
| Class | Purpose |
|---|---|
| ifstream | For reading from files |
| ofstream | For writing to files |
| fstream | For both read and write |
Include from <ios> (indirect via <fstream>)
| Mode | Meaning |
|---|---|
| ios::in | Open for reading (default for ifstream) |
| ios::out | Open for writing (default for ofstream) |
| ios::app | Append to end |
| ios::ate | Move to end after opening |
| ios::trunc | Truncate existing file |
| ios::binary | Binary mode |
Combine modes using | (bitwise OR)
fstream file("data.txt", ios::in | ios::out | ios::app);