Page 117 - C++
P. 117

public:
               void EnterData() {cin>>Rno; cin.getline(Name,20);
               void ShowData() {cout<<Rno<<” - ”<<Name<<endl;}
               };
        With reference to this information, write output of the following program segment:
               ifstream File; Student S;
               File.open(“STUDENTS.DAT”,ios::binary|ios::in);
               File.seekg(0, ios::end);
               Cout<<File.tellg();

        Ans:   220     because total 10 records each of 22 bytes , when open a file in reading mode the file pointer
                   th
        comes to 0  byte and ios::end will take the pointer to the end and will return the byte no. at end.

        (b) Write a function in C++ to count the number of lines starting with a digit in a text file “DIARY.TXT”.
        Ans:
               void abc()
        {      int c=0;
               char line[80];
               ifstream fin(“diary.txt”);
               if(!fin)  {cout<<”sorry”;exit(0);}
               while(!fin.eof())
               {
                       fin.getline(line,80);
                       if(isdigit(line[0]);
                       c++;
               }
               fin.close();
        }

        (c) Given a binary file “STUDENT.DAT”, containing records of the following class Student type:
        class student
        { char S_admno[10]; //Admission no. of student
        char S_Name[20]; //Name of student
        int Percentage; //Marks percentage of student
        public:
        void EnterData()
        { gets(S_admno); gets(S_Name); cin>>Percentage;
        }
        void DisplayData()
        { cout<<setw(12)<<S_admno;
        cout<<setw(32)<<S_Name;
        cout<<setw(3)<<Percentage<<endl;
        }
        int Ret_Per() {return Percentage;}
        };
        Write a function in C++ that would read contents of the file “STUDENT.DAT” and display the details of
        those students whose percentage is above 75.
        Ans:
          void read()
               {       student s1;
                       ifstream fin(“student.dat”, ios::binary);
                       if(!fin)  {cout<<”sorry”;exit(0);}
               while(!fin.eof())
               {       fin.read((char*)&s1, sizeof(s1));
                       if(s1.ret_per()>75)
   112   113   114   115   116   117   118   119   120   121   122