Saturday, 19 August 2017

CBSE Class 12 - Computer Science - C++ Practical Binary File Processing Snippet-10 Copy Records (#cbseNotes)

C++ Practical
Binary File Processing Snippet-10
Copy Records 

CBSE Class 12 - Computer Science - C++ Practical Binary File Processing Snippet-10 Copy Records  (#cbseNotes)

Question: Given a binary file TELEPHON.DAT, containing records of the following class Directory

class Directory
  {
    char name[20];
    char address[30];
    char areacode[5];
    char phonenum[15];
    public:
      void register();
      void show();
      int checkcode(char AC[])
      { return strcmp(areadcpde, AC); }
   };

 Write a function COPYABC() in C++ that would copy all the records have areacode as "123" from TELEPHON.DAT to TELEBACK.DAT

Answer:




//Question: Given a binary file TELEPHON.DAT, containing records of the following class Directory
//  class Directory
//  {
//    char name[20];
//    char address[30];
//    char areacode[5];
//    char phonenum[15];
//    public:
//      void register();
//      void show();
//      int checkcode(char AC[])
//      { return strcmp(areadcpde, AC); }
//   };
// Write a function COPYABC() in C++ that would copy all the records have areacode as "123"
// from TELEPHON.DAT to TELEBACK.DAT

#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <stdio.h>
#include <process.h>

void COPYABC();

class Directory
{
    char name[20];
    char address[30];
    char areacode[5];
    char phonenum[15];
    public:
      Directory();
      void addrec();
      void show();
      int checkcode(char AC[])
      { return strcmp(areacode, AC); }
};

Directory::Directory()
{
 strcpy(name, "");
 strcpy(address, "");
 strcpy(areacode, "");
 strcpy(phonenum, "");
}

void Directory::addrec()
{
 cout << "Enter Name: ";
 gets(name);
 cout << "Enter address: ";
 gets(address);
 cout << "Enter Areacode: ";
 gets(areacode);
 cout << "Enter Phone Number:";
 gets(phonenum);

 // open file and append record
 ofstream ofile("TELEPHON.DAT",ios::app|ios::binary);
 ofile.write((char *)this,sizeof(Directory));
 ofile.close();
}

int main()
{
  cout << "Enter 4 phone records:\n";
  Directory d;
  d.addrec();
  d.addrec();
  d.addrec();
  d.addrec();
  COPYABC();
  return 0;
}

void COPYABC()
{
 ifstream ifile("TELEPHON.DAT",ios::in|ios::binary);
 if(!ifile)
 {
  cout<<"could not open TELEPHON.DAT";
  exit(1);
 }
 
 ofstream ofile("TELEBACK.DAT",ios::out|ios::binary);
 
 if(!ofile) 
 {
  cout<<"could not open TELEBACK.DAT"; 
  exit(-1);
 }
 
 Directory d;
 int num = 0;
 cout << "Copying...\n";
 while(ifile.read((char *)&d, sizeof(d)))
 {
  if(d.checkcode("123")==0)
     {
     num++;
     ofile.write((char *)&d,sizeof(d));
     }
 }
 cout << num << " record(s) copied.\n";
 
 ifile.close();
 ofile.close();
}


Program Output:

CBSE Class 12 - Computer Science - C++ Practical Binary File Processing Snippet-10 Copy Records  (#cbseNotes)

No comments:

Post a Comment

We love to hear your thoughts about this post!

Note: only a member of this blog may post a comment.