file handling programs in C++

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<fstream.h>
#include<stdlib.h>
#include<stdio.h>
#include<iomanip.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<<"nnntttThis program clear some concept of file handlingn";
  cout<<"nnttt";
  system("pause");
}

write record into file (database) using ofstream stream-object

//get record by end user
void student::get_rec(void)
{
  clrscr();
  ofstream fout;
  cout<<"INSERT RECORDn";
  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<<"nntRecord saved successfully...";
}

// create a architecture for display record into monitor

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

read a file using ifstream stream object

//display all record into monitor screen
void student::show_all(void)
{
  clrscr();
  ifstream fin;
  cout<<"SHOW ALL RECORDn";
  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 view this, where you will found How to set filename, remove file, rename file

void student::delete_()
{
  int x,true=0;
  clrscr();
  ofstream fout;
  ifstream fin;
  cout<<"DETELE RECORDn";
  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 student record using their roll no

void student::search()
{
  clrscr();
  int x;
  ifstream fin;
  cout<<"SEARCH RECORDn";
  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<<"nntRecord 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 RECORDn" ;
  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<<"nnEnter new recordnn";
     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 PROGRAMn";
  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<<"nntTotal 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.