Advertisement Remove all ads

Write a Definition for a Function Totalteachers( ) in C++ to Read Each Object of a Binary File Schools.Dat, Find the Total Number Teachers, Whose Data is Stored in the File and Display the Same. Assume that the File Schools.Dat is Created with the Help Objects of Class Schools, Which is Defined Below : - Computer Science (C++)

Write a definition for a function TotalTeachers( ) in C++ to read each object of a binary file SCHOOLS.DAT, find the total number teachers, whose data is stored in the file and display the same. Assume that the file SCHOOLS.DAT is created with the help objects of class SCHOOLS, which is defined below :

class SCHOOLS
{
    int SCode;        //School Code
    char SName[20];   //School Name
    int NOT;          // Name of Teachers in the school

public:
    void Display()
    {
       cout<<SCode<<"#"<<SName<<"#" << NOT << endl;
       int RNOT() {return NOT;}
};
Advertisement Remove all ads

Solution

void putnot()
{
    cout <<NOT<< "  ";
}

void TotalTeachers()
{ 
     fstream f;
     SCHOOLS Stu;
     f.open("SCHOOLS.DAT", ios::in|ios::binary);
     cout << “\nTotal no. of Teachers : \n”;
     while((f.read((char*)&Stu, sizeof(Stu))) != NULL)
     Stu.putnot();
     f.close();
}
Concept: Implementation of Basic File Operations on Text and Binary File in C++
  Is there an error in this question or solution?
Advertisement Remove all ads
Advertisement Remove all ads
Advertisement Remove all ads
Share
Notifications

View all notifications


      Forgot password?
View in app×