File Handling in C++
What is File Handling?
File handling allows you to store data permanently in files instead of losing it when the program ends.
google.com, pub-8434817042454839, DIRECT, f08c47fec0942fa0
Why File Handling?
- Save data permanently
- Read stored data later
- Used in real applications
Types of Files
- Text files
- Binary files
Example (Writing to File)
#include<iostream>
#include<fstream>
using namespace std;
#include<fstream>
using namespace std;
int main() {
ofstream file(“data.txt”);
file << “Hello World”;
file.close();
}
Example (Reading from File)
#include<iostream>
#include<fstream>
using namespace std;
#include<fstream>
using namespace std;
int main() {
ifstream file(“data.txt”);
string data;
file >> data;
cout << data;
file.close();
}
Real-Life Use
Saving user data, logs, reports
Interview Question
Difference between text and binary files?
Summary
File handling helps in storing and retrieving data.

