English

Write a Definition for Function Countpics() in C++ to Read Each Object of a Binary File Photos.Dat, Find and Display the Total Number of Photos of Type Portrait. Assume that the File Photos.Dat is Created with the Help of Objects of Class Photos, Which is Defined Below - Computer Science (C++)

Advertisements
Advertisements

Question

Write a definition for function COUNTPICS() in C++ to read each object of a binary file PHOTOS.DAT, find and display the total number of PHOTOS of type PORTRAIT. Assume that the file PHOTOS.DAT is created with the help of objects of class PHOTOS, which is defined below :

class PHOTOS
{
   int PCODE ;
   char PTYPE[20]; //Photo Type as "PORTRAIT" ,"NATURE"
public:
   void ENTER()
   {
       cin>>PCODE;gets(PTYPE);
   }
   void SHOWCASE()
   {
       cout<<PCODE<<":" <<PTYPE<<endl;
   }
   char *GETPTYPE(){return PTYPE;}
};

Solution

void COUNTPICS()
{ 
   PHOTOS P;
   fstream fin;
   fin.open("PHOTOS.DAT", ios::binary|ios::in);
   char count = 0;
   while(fin.read((char*)&P,sizeof(P)))
   {
       if (P.WhatType() == 'PORTRAIT')
       count ++;
   }
   fin.close();
   cout << "Total number of photos of type PORTRAIT is " << count;
}
shaalaa.com
  Is there an error in this question or solution?
2016-2017 (March) All India Set 4
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×