What is a File?
A file is a storage unit on a computer that holds data permanently.
Unlike variables, which are stored temporarily in RAM, files are stored on hard disk, SSD, or external storage.
Real-World Analogy:
File = Notebook
Pages in the notebook = Lines of data
You can write, read, and update the notebook anytime
Why files are needed:
Persistence → Data remains after program ends
Large storage → Store more data than memory allows
Sharing → Data can be shared with other programs
File Concepts in C++
Types of Files
| Type | Description | Example |
|---|---|---|
| Text File | Stores data as readable text | .txt, .csv |
| Binary File | Stores data in binary form | .bin, .dat |
Text File Example:
Binary File Example:
Stores actual memory data
Not human-readable
Faster for computer processing
File Operations in C++
Opening a File → Connect your program to the file
Closing a File → Disconnect the file
Reading from a File → Get data from the file
Writing to a File → Send data to the file
File Streams
C++ provides fstream library to work with files:
| Stream Class | Purpose |
|---|---|
ofstream | Write to a file |
ifstream | Read from a file |
fstream | Both read and write |
Include header:
Writing to a File
Syntax:
Full Example 1:
Practice Exercise:
Write a program to store 5 student names and marks in a file
students.txt.
Reading from a File
Syntax:
Full Example 2:
Practice Exercise:
Read the student data from
students.txtand display only students with marks > 18.
Opening Modes
| Mode | Symbol | Description |
|---|---|---|
| Output | ios::out | Write to file (overwrite) |
| Input | ios::in | Read from file |
| Append | ios::app | Add data at the end |
| Binary | ios::binary | Open file in binary mode |
| Read & Write | ios::in | ios::out |
Example of Append Mode:
Binary Files (Optional Deep Info)
Binary files store raw memory data
Faster than text files
Used for large datasets, images, programs
Example:
Explanation:
reinterpret_cast<char*>converts memory address to char pointersizeof(num)ensures correct number of bytes
Advantages of Using Files
Data persists after program ends
Large storage → no memory limitation
Data sharing between programs
Logging and record keeping
Limitations of Files
Slower than memory operations → accessing hard disk takes time
File corruption risk → if program crashes while writing
Complex management → need to open, close, check errors
Not real-time → cannot instantly update like variables
Real-World Analogies
| Concept | File Analogy |
|---|---|
| File | Notebook |
| Writing | Pen writing on notebook |
| Reading | Reading notebook |
| Append | Adding more pages at the end |
| Binary File | Secret encrypted notebook, readable only by program |
Extra Practice Exercises
Write Student Info:
Create
students.txtStore name, age, and grade for 5 students
Read & Filter:
Read
students.txtDisplay students with grade ‘A’ only
Append Data:
Add new student info without overwriting old data
Binary Data Storage:
Store an integer array into
numbers.binRead it back and display