File Handling Examples in C++

Here in this is page we will discuss Insert Record ,Display Record , Search Record, Delete Record, Modify Record and Programs Properties.

Let’s Start Here,

The program has been expend (extra code of line) so that it is easily understood. Each option is placed in different blocks to make the program readable. To get better output of the program, execute turbo on C++.

It has the Selection menu as follows-

insert record
Display record
Search record
Modify record
Delete record
Properties
Exit

If you want, you can improve it further by adding other options like Backup database, format database, recover database, user login etc. This will clear your file handling and concept as well as improve your programming skill. Which is better than clearing the concept.

Here is the program,

// header file include
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<iomanip.h>
#include<fstream.h>
//data member and member function declartion

Creating a class name as student

class student
{
int roll;char sname[29];
char fname[20],mname[20];
public: // function declaration in public mode student(void); // constructor declaration for intro wizard ~student(void){ } // destroy object initialized by constructor void get_rec(void); // write record into file void show_all(void); // display all record void putdata(void); // create a template to displaying record on monitor void search(); // search a specific record void total(); // properties of program void modify_rec(void); // create a template to modifying record void modify(); // modify record into file void delete_(); // delete spacific record }obj; // constructor called automatically
student::student(void)
{ 
  clrscr();
  cout<<"/t/t/tThis program clear some concept of file handling/n";
  cout<<"/t/t";
  system("pause");
}

Insert Record into file (database) using ofstream stream-object

//get record by end user
void student::get_rec(void)
{
  clrscr();
  ofstream fout;
  cout<<"INSERT RECORD/n";
fout.open("database",ios::app|ios::binary); cout<<"Enter roll no: "; cin>>roll; cout<<"Enter stuname: "; gets(sname); cout<<"Enter father name: "; gets(fname); cout<<"Enter mother name: "; gets(mname);
fout.seekp(0,ios::beg); //go to start fout.write((char*)&obj,sizeof(obj)); fout.close();
cout<<"/n/n/tRecord saved successfully..."; }

// create a architecture for Display Record into monitor

void student::putdata(void)
{
    cout<<roll<<"/t"<<sname<<"/t"<<fname<<"/t"<<mname<<endl;
}

Display Record using ifstream stream object

//display all record into monitor screen
void student::show_all(void)
{
  clrscr();
  ifstream fin;
  cout<<"SHOW ALL RECORD/n";
fin.open("database",ios::app|ios::binary); fin.seekg(0,ios::beg); //goto begining or start
while(fin.read((char*)&obj,sizeof(student))) { putdata(); } fin.close();
getch(); //hold screen }

Delete Record from file using remove and rename the file

for more File Handling in C++, where you will found How to give filename by user, remove a file, rename a file

void student::delete_()
{
  int x,true=0;
  clrscr();
  ofstream fout;
  ifstream fin;
  cout<<"DETELE RECORD/n";
cout<<"Enter student roll no: "; cin>>x;
int r = x; fin.open("database",ios::binary); fout.open("temp",ios::binary);
while(fin.read((char*)&obj,sizeof(obj))) { if(x==r) { if(x!=roll) // which record not matched write into temp file { fout.write((char*)&obj,sizeof(student)); true=1; } } } fout.close(); fin.close();
remove("database"); //remove file rename("temp","database"); //rename file
if(true) cout<<"record deleted..."; if(true!=1) cout<<"record not found....."; }

Search Specific Record using Students roll-no

void student::search()
{
  clrscr();
  int x;
  ifstream fin;
  cout<<"SEARCH RECORD/n";
  cout<<"Enter student roll no: "; 
  cin>>x;
fin.open("database",ios::app|ios::binary); while(fin.read((char*)&obj,sizeof(obj))) { if(x==roll) { putdata(); } } fin.close(); getch(); }

 create a Architecture To Enter New Record

void student::modify_rec(void)
{
  cout<<"Enter new roll no: "; 
  cin>>roll;
  cout<<"Enter new stuname: ";
  gets(sname);
  cout<<"Enter new father name: ";
  gets(fname);
  cout<<"Enter new mother name: ";
  gets(mname);
cout<<"/n/ntRecord updated successfully..."; }

Modify a Specific Record in a file

using int variable=(object-1)*sizeof(class-name/class-object); with manipulator

// modify specific student record using their roll no
void student::modify()
{
  clrscr();
  int x; char true=0;
  fstream file;
  cout<<"MODIFY RECORD/n" ;
  cout<<"Enter roll no: "; 
  cin>>x;
file.open("database",ios::in|ios::out|ios::binary); while(file.read((char*)&obj,sizeof(student))) { if(x==roll) { cout<<"/nOld record are n"; putdata(); cout<<"/n/nEnter new record/n/n";
modify_rec();
int loc=(-1)*sizeof(obj); file.seekp(loc,ios::cur); // give current found location into file file.write((char*)&obj,sizeof(student)); true=1; } } file.close(); if(!true) //true!=1 cout<<"record not found....."; getch(); }

Calculate Total Record into file using manipulators

// show properties of program like total record and size of file
void student::total()
 {
   clrscr(); 
   cout<<"ABOUT PROGRAM/n";
   int total_rec,st;
   ifstream fin;
   fin.open("database",ios::in|ios::out|ios::binary);
/*******using manipulators*********/ fin.seekg(0,ios::end); // goto end of file total_rec=fin.tellg()/sizeof(student); st = fin.tellg();
cout<<"/n/ntTotal record in file of student are: "<<total_rec; cout<<"/ntMemory of database is: "<<st<<" byte";
_setcursortype(_NOCURSOR); // disable mouse cursor getch(); }

main Program Start Here

void main()
{
  char ch;
  do
  {
  clrscr();
  gotoxy(40,5);// print message a specific location into monitor
  cout<<"SELECTION MENUn";
  gotoxy(40,7);
  cout<<"1.INSERT RECORDn";
  gotoxy(40,8);
  cout<<"2.DISPLAY RECORDn";
  gotoxy(40,9);
  cout<<"3.SEARCH RECORDn";
  gotoxy(40,10);
  cout<<"4.MODIFY RECORDn";
  gotoxy(40,11);
  cout<<"5.DELETE RECORDn";
  gotoxy(40,12);
  cout<<"6.PROPERTIESn";
  gotoxy(40,13);
  cout<<"0.EXITn";
  gotoxy(45,18);
  cout<<"Enter Your Choice: ";
  ch=getche();
  switch(ch)
  {
    case '1': obj.get_rec();break;
    case '2': obj.show_all();break;
    case '3': obj.search(); break;
    case '4': obj.modify(); break;
    case '5': obj.delete_();break;
    case '6': obj.total();break;
    case '0': exit(0);               //program exit
  }
 }
 while(ch!=0);
 getch();
}
OUTPUT:-
        SELECTION MENU
        1.INSERT RECORD
        2.DISPLAY RECORD
        3.SEARCH RECORD
        4.MODIFY RECORD
        5.DELETE RECORD
        6.PROPERTIES
        0.EXIT
             Enter Your Choice:

Now, you are ready to make your own project………

Back to exercise

Here are some related project.