Data File Handling | Class 12 ASSEB Computer Science Notes

Chapter 6 - Data File Handling in C++

Introduction

Imagine a student management program where you enter student names and marks. Everything works perfectly while the program is running.

But what happens when you close the program?

All the information disappears.

To save information permanently, C++ uses files.

A file allows data to be stored permanently on secondary storage devices such as the hard disk or SSD. The stored information can later be read, modified or deleted whenever required.

What is File Handling?

Definition

File handling is a process that allows a C++ program to store data permanently in a file and retrieve it later when needed. Unlike RAM where data is stored temporarily, data stored in files remain available even after a program terminates or the computer is switched off.

Need for File Handling

Without files

  • Data is lost after program execution.
  • Records cannot be reused.

With files

  • Large amounts of data can be stored permanently.
  • Information can be updated later.
  • Records can be searched easily.
  • Data can be shared between different programs

Application of File Handling

File handling is used in

  • School Management System
  • Library Management
  • Hospital Records
  • Employee Database
  • Railway Reservation

Types of Files

Files are mainly of two types.

1. Text Files

Data is stored in the form of characters

Example

Rahul

85

A

Characteristics

  • Human readable
  • Easy to edit
  • Slightly slower
  • Larger file size

2.Binary Files

Data is stored in the form of bytes (0 and 1).

Characteristics

  • Faster
  • Compact
  • Cannot be read directly using Notepad
  • Suitable for objects

File Stream Classes

C++ provides three important classes. The fstream header file provides three classes to perform file operations

file-stream-classes

Class

Purpose

ifstream

Used to read data from a file

ofstream

Used to write data into a file

fstream

Used to read from and write into a file

Opening a Fiie

There are two methods.

Method 1 – using open function

ofstream fout;

fout.open(“student.txt”);

Method 2- Using constructor

ofstream fout(“student.txt”);  //for writing

ifstream fin;                  // for reading

fstream f; 

Closing a File

Always close the file after performing operations using the close( ) function.

Example

fout.close();

fin.close();

f.close();

File opening modes

Files can be opened in different modes. Some common modes are:

Mode

Meaning

ios::in

Open file for reading

ios::out

Open file for writing

ios::app

Open file in Append mode

ios::ate

Open file and move pointer to the end of file

ios::binary

Open file in Binary mode

ios::trunc

If file exists its contents are deleted

End of File

When the end of a file is reached , the stream returns a special value called EOF (End of File)

Methods to detect end of file

1.fin.eof( ) : Returns true if end of file is reached

2.fin.fail( ) : Returns true if any input operation fails (including EOF)

Commonly used functions for file handling

1.get(char & ) – reads one character from file

2.put(char) – writes one character into file

3.getline( ) – reads a line from file (upto newline)

4.read(char*,n) – reads n bytes from binary file

5.write(char*,n) – writes n bytes into binary file

Random Access in Files

Random access means accessing any record directly without reading all previous records. We can move the read/write pointer to any position in a file using the following functions:

1.seekg(pos): moves the get pointer to pos position

2.seekp(pos):moves the put ponter to pos position

3.tellg()Returns current position of get pointer

4.tellp()Returns current position of put pointer

Working with Binary Files

Binary files store data in the form of bytes. We use the read() and write( ) functions to deal with binary files. Suppose we have

class Student

{

public:

    int roll;

    char name[30];

};

Writing object

Student s;

ofstream fout(“student.dat”,ios::binary);

fout.write((char*)&s,sizeof(s));

fout.close();

Reading object

ifstream fin(“student.dat”,ios::binary);

fin.read((char*)&s,sizeof(s));

fin.close();

Program to write student details

#include<iostream>

#include<fstream>

using namespace std;

int main()

{

    ofstream fout(“student.txt”);

    string name;

    int marks;

    cout<<“Enter Name : “;

    cin>>name;

    cout<<“Enter Marks : “;

    cin>>marks;

    fout<<name<<endl;

    fout<<marks;

    fout.close();

    cout<<“Record Saved.”;

    return 0;

}

Program to read student details

#include<iostream>

#include<fstream>

using namespace std;

int main()

{

    ifstream fin(“student.txt”);

    string name;

    int marks;

    fin>>name;

    fin>>marks;

    cout<<“Student Name : “<<name<<endl;

    cout<<“Marks : “<<marks;

    fin.close();

    return 0;

}

Quick Revision

Previous Chapter
Scroll to Top