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?
APPEARS IN
Advertisement Remove all ads