🔵 Core C++ · Lesson 36
File Handling in C++
File Handling in C++
What is File Handling in C++?
File Handling in C++
File handling allows programs to read and write data permanently using files. C++ provides ifstream, ofstream and fstream.
File handling allows programs to read and write data permanently using files. C++ provides ifstream, ofstream and fstream.
Level
🔵 Core C++ Features
🔵 Core C++ Features
Example File
file-handling.cppMain Focus
Concept + syntax + practical C++ program
Concept + syntax + practical C++ program
Why should you learn this?
- It helps you write correct and readable C++ programs.
- It is used repeatedly in school practicals, projects and competitive programming.
- It builds the base for advanced topics such as OOP, STL and data structures.
Important Terms
| Term | Meaning / Use |
|---|---|
| ifstream | Ifstream used in File Handling in C++ programming. |
| ofstream | Ofstream used in File Handling in C++ programming. |
| fstream | Fstream used in File Handling in C++ programming. |
| file | File used in File Handling in C++ programming. |
| stream | Stream used in File Handling in C++ programming. |
Syntax / Pattern
ofstream fout("file.txt"); fout << data;
Example Program
#include <iostream>
#include <fstream>
using namespace std;
int main(){
ofstream fout("result.txt");
fout << "Marks: 95";
fout.close();
cout << "File written";
return 0;
}
Expected Output
File written
Program Explanation
- ofstream opens file for writing.
- close() releases the file resource.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.
Where will you use it?
- saving reports
- student records
- logs
Common Mistakes
- Not checking whether file opened successfully.
- Using wrong file path.
Practice Tasks
- Write student marks to a file.
- Read file line by line using getline.
Summary
File Handling in C++ is an important C++ topic. Learn the definition, understand the syntax, run the example program and then solve the practice tasks to make the concept strong.
What is File Handling in C++?
File Handling in C++
File handling allows programs to read and write data permanently using files. C++ provides ifstream, ofstream and fstream.
File handling allows programs to read and write data permanently using files. C++ provides ifstream, ofstream and fstream.
Level
🔵 Core C++ Features
🔵 Core C++ Features
Example File
file-handling.cppMain Focus
Concept + syntax + practical C++ program
Concept + syntax + practical C++ program
Why should you learn this?
- It helps you write correct and readable C++ programs.
- It is used repeatedly in school practicals, projects and competitive programming.
- It builds the base for advanced topics such as OOP, STL and data structures.
Important Terms
| Term | Meaning / Use |
|---|---|
| ifstream | Ifstream used in File Handling in C++ programming. |
| ofstream | Ofstream used in File Handling in C++ programming. |
| fstream | Fstream used in File Handling in C++ programming. |
| file | File used in File Handling in C++ programming. |
| stream | Stream used in File Handling in C++ programming. |
Syntax / Pattern
ofstream fout("file.txt"); fout << data;
Example Program
#include <iostream>
#include <fstream>
using namespace std;
int main(){
ofstream fout("result.txt");
fout << "Marks: 95";
fout.close();
cout << "File written";
return 0;
}
Expected Output
File written
Program Explanation
- ofstream opens file for writing.
- close() releases the file resource.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.
Where will you use it?
- saving reports
- student records
- logs
Common Mistakes
- Not checking whether file opened successfully.
- Using wrong file path.
Practice Tasks
- Write student marks to a file.
- Read file line by line using getline.
Summary
File Handling in C++ is an important C++ topic. Learn the definition, understand the syntax, run the example program and then solve the practice tasks to make the concept strong.