🔵 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.
Level
🔵 Core C++ Features
Example File
file-handling.cpp
Main Focus
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

TermMeaning / Use
ifstreamIfstream used in File Handling in C++ programming.
ofstreamOfstream used in File Handling in C++ programming.
fstreamFstream used in File Handling in C++ programming.
fileFile used in File Handling in C++ programming.
streamStream 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

  1. Write student marks to a file.
  2. 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.
Level
🔵 Core C++ Features
Example File
file-handling.cpp
Main Focus
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

TermMeaning / Use
ifstreamIfstream used in File Handling in C++ programming.
ofstreamOfstream used in File Handling in C++ programming.
fstreamFstream used in File Handling in C++ programming.
fileFile used in File Handling in C++ programming.
streamStream 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

  1. Write student marks to a file.
  2. 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.

← Back to C++ Tutorial