Topic :File Handling: Write Record | Modify Record | Search Record | Delete Record



InfoBrother DMS:

Upto this point, we have enough tools in our tool kit to create any advance program and store data in disk. now we are going to create a Program "InfoBrother DMS", This Program will be able to write record to File, Read record from file, Modify existing record, search record from file and delete any record from file: In this tutorial we will discuss some methods that we will use in our Program. so here we go:

image: infobrother.com



Download the complete source code of DMS project using the above button. compile it, run it, and try it yourself. there are some methods that we used in our project, that we need to discuss. so let's discuss, how we wrote this program, and what we did in the program.



  • Our DMS Is able to perform the following given Operations:

    » Create New Record:
    » Display All Stored Record.
    » Modify any Record.
    » Search for any record.
    » Delete any Record.



In our program, there are two classes that we create, the first class will take the user data. and the second class will store that data into a file. In order to display the stored data, the first class will retrieve the information from the file. let's discuss some of the important methods of our project.



Get Employe ID:
/*This Pointer Function will store ID No of Employee*/
char *getEmp_ID()
{
   return employe_ID; //return employee id
}

The above function is an Pointer function that points to an char. *getEmp_ID will point to the ID of Employee (i.e. empolye_ID). When user will enter the employee ID, then this pointer will store its location, and will provide the value or requested.



Write Data into the file:
void WriteEmployee()
{
    int more; //for loop.
    fp.open("Employe.DAT",ios::out | ios::app); 
    if(!fp) // error checker
    {
      	cout<<"\n\a Error...!! Employee file could not open";
        getch(); //get character:
        return;
    }	 
 
    do //loop: to repeat data until you press 2:
    {
        Emp.getEmp_data(); //funtion calling.
        fp.write((char*)&Emp,sizeof(Emp));
        cout<<"\n\n\a Press 1 to Enter More Data: "<<endl;
        cout<<" Press 2 to Return Main menu: "<<endl;
        cout<<" Option: ";
        cin>>more;
    }
    while(more==1); //Enter data again while enter no is 1;
    fp.close();
 
} 

The Above function writeEmployee() will open the file using file pointer fp, in Appending mode. if the file not exists, the pointer will create new file. Using Do-while loop , our program will write the entered information into the file with the help of Write() method.



Modify Previous Record.
// This Function will be used to Modify Employee Data:
void W_ModifyEmp()
{
   int found=0; //if we found data then return 1, else 0;
   char n[20]; //for getting user id.
 
   cout<<"\n\n\a Enter User ID"<<endl;
   cout<<" \nUser ID: ";
   cin>>n;
 
   fp.open("Employe.DAT",ios::in | ios::out);
   if(!fp) //error checker:
    {
         cout<<"\n\a Error...!! Employee file could not open";
         getch(); //get character:
         return;
     }
 
    while(fp.read((char*)&Emp, sizeof(Emp)))
    {
        if(strcmpi(Emp.getEmp_ID(),n)==0) //if enter id matched with any data
        {
             Emp.showemp_data(); //show the record:
             cout<<"\n Enter New Data To Modify: "<<endl;
             Emp.modifyEmp_data();
             int position=-1*sizeof(Emp); //start from current postion to backward.
             fp.seekp(position,ios::cur); //set writing pointer to curr position.
             fp.write((char*)&Emp,sizeof(Emp));
             found=1; //we found data, so found is set to 1,
         }
      }
      fp.close();
      if(found==0)
       {
           cout<<"\n\n\a Record Not found: "<<endl;
       }
       else
       cout<<"\n\t\a Employee Data Modify Successfully:"<<endl;
       getch();
}

This is an Important function and litbit confusing. what i did here, I just ask from user to enter the ID to search for record, the program store the ID in Variable n then we open the file for reading and by using String Compare function strcmpi() we try to compare the ID store in variable n and ID store in Pointer variable getEmp_ID. if the ID matched, then we can Open that record and can modify it. Now using seekp() function, we set the position of file-pointer to specific location (at the starting of that record) and modify Overwrite the record. we used another variable found. We find the record and modify it. so assign variable found with 1; now the value of found is 1. but if its still 0(zero), it's mean the program can't found the record.



Search any Record from file.
//By this function we can find any data from all stored data:
void W_SearchEmp()
{
    int found=0; //if we found record then return 1. else 0;
    char n[20]; //to get employee id from user
 
    cout<<"\n\n\a Enter Employee ID: "<<endl;
    cout<<" Employee ID: ";
    cin>>n;
 
    fp.open("Employe.DAT",ios::in);
    if(!fp) //error chacker
    {
       	cout<<"\n\a Error...!! Employee file could not open";
       	getch();
       	return;
    }
 
    while(fp.read((char*)&Emp,sizeof(Emp)))
    {
         if(strcmpi(Emp.getEmp_ID(),n)==0) //if entered id will match with any data.
         {
             Emp.showemp_data(); //calling function
             found=1; //we found data so found is set to 1.
         }
    }
    fp.close();
    if(found==0) //uf we didnt found record then found will be 0;
    {
        cout<<"\n\n\a Record Not found: "<<endl;
    }
     getch();
}

Using Above function we can search any stored record from the file. This function compare the user entered ID and getEmp_ID using String Compare function strcmpi(). if Program found the record, it will display the record on screen and assign 1 to variable n. if the variable n is still 0(zero) its mean we didn't found the record.



Delete any Record from file.
// This function will be used to delete any record
void deleteEmp()    
{
     int found=0; //if we found data the return 1. else 0;
     char n[20]; //for getting user id from user.
 
     cout<<"\n\n\a Enter User ID: "<<endl;
     cout<<" User ID: ";
     cin>>n;
 
     fp.open("Employe.DAT",ios::out | ios::in);
     if(!fp) //error chacker
     {
         cout<<"\n\a Error...!! Employee file could not open";
         getch();
         return;
     }
 
    fstream fp2; //temperory file pointer
    fp2.open("temp.DAT", ios::out);
     if(!fp2) //error chacker
     {
         cout<<"\n\a Error...!! Temperary file could not open";
         getch();
         return;
     }
 
    fp.seekg(0,ios::beg); //set reading pointer at the, beganing
    while(fp.read((char*)&Emp,sizeof(Emp)))
    {
       /*read all data and if entered id will match the any 
         stored id then lev this and get all other data.*/
 
        if(strcmpi(Emp.getEmp_ID(),n)!=0) 
        {
          //and copy all data to temp file except the matched one.
            fp2.write((char*)&Emp,sizeof(Emp)); 
        }
 
        else //now we have got the temp file without the selected one record.
        found=1; //as we found the record so found is set to 1;
    }
 
    fp.close();
    fp2.close();
 
    remove("Employe.DAT"); //delete the Employee file
    rename("temp.DAT","Employe.DAT"); //rename the temp file with employee file.
    if(found==0) //if found = 0; then its mean we didnt found the record.
    {
        cout<<"\n\n\a Record Not Found: "<<endl;
    }
    else
    cout<<"\n\t\a Employee Data Delete Successfully:"<<endl;
    getch();
 
}

To Delete any record, first we need to search that record, that we already discuss above. We will search the Record, and will set the Input pointer at the beginning of the file using seekg() function. we will Open two files, First is the Existing file, where we store our record, and we will open it for reading purpose. and the second file will be temporary file for writing purpose. If the program the required data to delete, it will copy all the record to temperory file except the Matched one. Now we will delete the old file, and will rename the temporary file.











I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement