File Handling in C++
📘 C++
👁 26 views
📅 Dec 22, 2025
⏱ Estimated reading time: 2 min
File handling in C++ is used to store and retrieve data permanently from files.
File Stream Classes
C++ provides file stream classes in the header.
| Class | Purpose |
|---|---|
ifstream | Read from file |
ofstream | Write to file |
fstream | Read and write |
Opening a File
Syntax
Or
Writing to a File
Reading from a File
Reading a Full Line
File Opening Modes
| Mode | Description |
|---|---|
ios::in | Read mode |
ios::out | Write mode |
ios::app | Append mode |
ios::binary | Binary file |
ios::trunc | Clear file content |
Example:
Complete Example Program
Checking File Open Success
Advantages of File Handling
-
Permanent data storage
-
Easy data retrieval
-
Useful for large data
Key Points
-
is required -
Always close files
-
Use correct file modes
-
Supports text and binary files
Conclusion
File handling in C++ allows programs to store data permanently and retrieve it efficiently using file streams.
🔒 Some advanced sections are available for Registered Members
Register Now
Register Now
Share this Post
← Back to Tutorials