File I/O with
fstream
Learn to read from and write to text files using C++ file streams: the gateway to persistent data storage.
What Is File I/O?
File I/O stands for File Input/Output: the process of reading data from files and writing data to files on disk. Without file I/O, every variable you create vanishes when your program ends.
The <fstream> Header
C++ provides file stream classes in the <fstream> header. These classes work just like cin and cout, but connect to files instead of the console.
The Stream Metaphor
Think of a stream as a one-way pipe. An ifstream carries data from the file into your program. An ofstream carries data from your program into the file.
The Core Pattern
Every file operation in C++ follows three steps. This pattern applies whether you are reading or writing.
>>, <<, or getline() to transfer data.ifstream vs ofstream vs fstream
| ifstream | ofstream | fstream | |
|---|---|---|---|
| Direction | Read only | Write only | Read & Write |
| Default mode | ios::in | ios::out | None (specify) |
| Creates file? | No | Yes | Depends on mode |
| Overwrites? | N/A | Yes (default) | Depends on mode |
| Best for | Loading data | Saving data | Advanced use |
Step-by-Step Checklist
>>, <<, or getline().Example 1: Writing to a File
Example 2: Reading from a File
Example 3: Reading Word by Word
The extraction operator >> reads one whitespace-delimited token at a time. For a file containing "Hello World 42", this prints:
Appending with ios::app
By default, ofstream overwrites the file. To add to the end without erasing existing content, use the append mode flag.
Reading Numbers from a File
The >> operator automatically converts text to the target variable type. If your file has "3.14 42 100", you can read directly into numeric variables.
The File I/O Flow
getline() vs >>
| getline(stream, str) | stream >> var | |
|---|---|---|
| Reads | Entire line (until newline) | One token (until whitespace) |
| Includes spaces? | Yes | No |
| Stores into | string only | Any compatible type |
| Stops at | '\n' (consumed, not stored) | Stops at whitespace, leaves it in stream |
| Use when | Lines have spaces/mixed content | Data is space-separated tokens |
Common Mistakes
>> leaves '\n' in the buffer. The next getline() reads an empty string. Use fin.ignore() between them.Summary Reference
| Operation | Code | Notes |
|---|---|---|
| Include header | #include <fstream> | Required for all file I/O |
| Open for writing | ofstream fout("f.txt"); | Creates / overwrites |
| Open for reading | ifstream fin("f.txt"); | File must exist |
| Append mode | ofstream fout("f.txt", ios::app); | Adds to end |
| Read line | getline(fin, str); | Includes spaces |
| Read token | fin >> var; | Skips whitespace |
| Write data | fout << data << endl; | Like cout |
| Close file | fin.close(); | Always close |
Question 1 of 5
Which header file must you include to use ifstream and ofstream in C++?
Question 2 of 5
What happens by default when you open a file with ofstream and the file already exists?
Question 3 of 5
Which function reads an entire line (including spaces) from a file stream into a string?
>> operator stops at any whitespace, so it cannot read a full line with spaces.Question 4 of 5
Consider this code fragment:
ifstream fin("data.txt");
int x;
string name;
fin >> x;
getline(fin, name);
The file data.txt contains:
42
Alice Smith
What value does name hold after execution?
fin >> x reads "42" but leaves the newline '\n' in the buffer. The next getline() reads that leftover newline and returns an empty string. Fix this by calling fin.ignore(); between the two reads.Question 5 of 5
How do you open a file for writing without erasing its existing content?