File Handling through C++ Classes

In this tutorial, we will be discussing a program to understand File Handling through C++ classes.

The default functions used in file handling to interact with files can be defined by the user using classes. Below is the implementation of ifstream and ofstream functions.


#include <iostream>
#include <fstream>
using namespace std;
int main(){
   //creating ofstream object ofstream fout;
   string line;"sample.txt");
   //initiating loop if file is opened
   while (fout) {
      getline(cin, line);
      if (line == "-1")
      fout << line << endl;
   ifstream fin;"sample.txt");
   while (fin) {
      getline(fin, line);
      cout << line << endl;
   return 0;