Friday, December 27, 2013

Assignment 13: File Handling - Binary File

Question 1
Assuming the class EMPLOYEE given below, write functions in C++ to perform following:
(i) Write the objects of EMPLOYEE to a binary file.
(ii) Read the objects of EMPLOYEE from binary file and display them on screen.
class EMPLOYEE
{
            int ENO;
            char ENAME[10];
            public :
            void GETIT()
            {
                        cin >> ENO;
                        gets (ENAME);
            }
            void SHOWIT()
           {
                        cout <<ENO << ENAME <<endl;
            }
};
Question 2
Assuming the class Computer as follows :
class computer
{
            char chiptype[10];
            int speed;
            public:
                        void getdetails()
                        {
                                    gets(chiptype);
                                    cin>>speed;
                        }
                        void showdetails()
                        {
                                    cout<<“Chip”<<chiptype<<“ Speed= “<<speed;
                        }
};
Write a function readfile( ) to read all the records present in an already existing binary file SHIP.DAT and display them on the screen, also count the number of records present in the file.
Question 3
Given a binary file STUDENT.DAT, containing records of the following class Student type
class Student
{
            char S_Admno[lO];    //Admission number of student
            char S_Name[30];       //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 ReturnPercentage()
                        {return Percentage;}
};
Write a function in C++, that would read contents of file STUDENT.DAT and display the details of those Students whose Percentage is above 75.
Question 4
Observe the program segment given below carefully and fill the blanks marked as Statement 1 and Statement 2 using seekg() and tellg() functions for performing the required task.
#include <fstream.h>
class Employee
{
            int Eno;
            char Ename[20];
            public:
            //Function to count the total number of records
            int Countrec();
};
int Item::Countrec()
{
            fstream File;
            File.open("EMP.DAT",ios::binary|ios::in);
            ______________________ //Statement 1
            int Bytes =______________________ //Statement 2
            int Count = Bytes / sizeof(Item);
            File.close();
            return Count;
}
Question 5
Write a function in C++ to add new objects at the bottom of a binary file "STUDENT.DAT", assuming the binary file is containing the objects of the following class.
class STUD
{
            int Rno;
            char Name[20];
            public:
            void Enter()
            {cin>>Rno;gets(Name);}
            void Display()
            {cout<<Rno<<Name<<endl;}
};
Question 6
Observe the program segment given below carefully and fill the blanks marked as Statement 1 and Statement 2 using seekp() and seekg() functions for performing the required task.
#include <fstream.h>
class Item
{
            int Ino;
            char Item[20];
            public:
            //Function to search and display the content from a particular record number 
            void Search(int );
           //Function to modify the content of a particular record number
            void Modify(int);
 };
 void Item::Search(int RecNo)
{
            fstream File;
            File.open(“STOCK.DAT”,ios::binary|ios::in);
            ______________________ //Statement 1
            File.read((char*)this,sizeof(Item));
            cout<<Ino<<”==>”<<Item<<endl;
            File.close();
}
void Item::Modify(int RecNo)
{
            fstream File;
            File.open(“STOCK.DAT”,ios::binary|ios::in|ios::out);
            cout>>Ino;
            cin.getline(Item,20);
            ______________________ //Statement 2
            File.write((char*)this,sizeof(Item));
            File.close();
}

No comments:

Post a Comment