Wednesday, 26 July 2017

DATA FILE HANDLING

BINARY FILES

If you’re storing a large amount of numerical data it’s more efficient to use binary I/O, in which numbers are stored as they are in the computer’s RAM memory, rather than as strings of characters.

We use two new functions: write(), a member of ofstream; and read(), a member of ifstream. These functions think about data in terms of bytes (type char).

They don’t care how the data is formatted, they simply transfer a buffer full of bytes from and to a disk file.

 The parameters to write() and read() are the address of the data buffer and its
length.

 The address must be cast, using reinterpret_cast, to type char*, and the length is the
length in bytes (characters), not the number of data items in the buffer.

Program to write on to the binary file and read the same

#include <fstream.h>
#include <conio.h>
#include <stdio.h>
class student
{
int rollno;
char name[20];
float marks;
public:
void input()
{
cout<<"Enter rollno";
cin>>rollno;
cout<<"Enter name";
gets(name);
cout<<"Enter marks";
cin>>marks;
}
void display()
{
cout<<rollno<<" "<<name<<" "<<marks<<endl;}};
void WRITE()
{
student s;
ofstream f("student.dat");
char ans='y';
do
{
s.input();
f.write((char *)&s,sizeof(s));
cout<<"continue(y/n)?";
cin>>ans;
}
while(ans=='y'||ans=='Y');
}
void READ()
{
student s;
ifstream f("student.dat");
while(f.read((char *)&s,sizeof(s)))
{
s.display();
}}
void main()
{
clrscr();
WRITE();
READ();
getch();}

Output














BOARD PAPER - 2015

Write a definition for function Economic() in C++ to read each record of a binary file ITEMS.DAT, find and display those items, which costs less than 2500. Assume that the file ITEMS.DAT is created with the help of objects of class ITEMS, which is defined below:
class ITEMS
{
int ID;char GIFT[20]; float Cost;
public :
void Get()
{
cin>>CODE;gets(GIFT);cin>>Cost;
}
void See()
{
cout<<ID<<":"<<GIFT<<":"<<Cost<<endl;
}
float GetCost() {return Cost;}.



{
ITEMS I;

ifstream fin(“ITEMS.DAT”,ios::binary);
while (fin.read((char *)&I,sizeof(I)))
{
if(I.GetCost()<2500)
I.See();
}
fin.close();

}

Find the output of the following C++ code considering that the
binary file CLIENTS.DAT exists on the hard disk with records of 100
members.
class CLIENTS
{
int Cno;char Name[20];
public :
void In(); void Out();

void main{)
{
fstream CF;
CF.open("CLIENTS.DAT",ios:: binary| ios::in) ;
CLIENTS C;
CF.read((char*)&C,sizeof(C));
CF.read((char*)&C,sizeof(C));
CF.read((char*)&C,sizeof(C));
int POS=CF.tellg()/sizeof(C);
cout<<"PRESENT RECORD:"<<POS<<endl;
CF.close() ;

}

PRESENT RECORD: 3


BOARD PATTERN QUESTIONS (BINARY FILES)

1)Find the output of the following C++ code considering that the binary file CLIENTS.DAT exists on the hard disk with a data of 200 clients :  
class CLIENTS
{
int CCode;char CName[20];
public:
void REGISTER(); void DISPLAY();
};

void main()
{
fstream File;
File.open("CLIENTS.DAT",ios::binary|ios::in);
CLIENTS C;
File.seekg(6*sizeof(C));
File.read((char*)&C, sizeof(C));
cout<<"Client Number:"<<File.tellg()/sizeof(C) + 1;
File.seekg(0,ios::end);
cout<<" of "<<File.tellg()/sizeof(C)<<endl;
File.close();
}

Ans:
Client Number:8 of 200

2)Find the output of the following C++ code considering that the binary file
MEM.DAT exists on the hard disk with a data of 1000 members :  
class MEMBER
{
int Mcode;char MName[20];
public:
void Register();void Display();
};
void main()
{
fstream MFile;
MFile.open(“MEM.DAT”, ios::binary|ios::in);
MEMBER M;
MFile.read((char*)&M, sizeof(M));
cout<<”Rec:”<<MFile.tellg()/sizeof(M)<<endl;
MFile.read((char*)&M, sizeof(M));
MFile.read((char*)&M, sizeof(M));
cout<<”Rec:””<<MFile.tellg()/sizeof(M)<<endl;
MFile.close();
}

Ans:

Rec:1
Rec:3

3) Write a definition for function BUMPER() in C++ to read each object of a binary
file GIFTS.DAT, find and display details of those gifts, which has remarks as “ÖN
DISCOUNT”. Assume that the file GIFTS.DAT is created with the help of objects
of class GIFTS, which is defined below :  
class GIFTS
{
int ID;char Gift[20],Remarks[20]; float Price;
public:
void Takeonstock()
{
cin>>ID;gets(Gift);gets(Remarks);cin>>Price;
}
void See()
{
cout<<ID<<”:”<<Gift<<”:”<<Price<<””:”<<Remarks<<endl;
}
char *GetRemarks() {return Remarks;}
};


Ans:

void Bumper()
{
GIFTS G;
ifstream f("GIFTS.DAT");
int flag=0;
while(f.read((char *)&f,sizeof(G)))
{
if (strcmpi(G.GetRemarks(),"ON DISCOUNT")==0)
   {G.See();flag=1;}
}
if(flag==0) cout<<"Record not found";}

4) 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;}};

5) Write a definition for function COUNTDEPT( ) in C++ to read each object of a binary file TEACHERS.DAT, find and display the total number of teachers in the department MATHS. Assume that the file TEACHERS.DAT is created with the help of objects of class TEACHERS, which is defined below :  
class TEACHERS
{
int TID; char DEPT[20];
public:
void GET()
{
cin>>TID;gets(DEPT);
}
void SHOW()
{
cout<<TID<<":"<<DEPT<<end1;
}
char *RDEPT(){return DEPT;}};




6) Write a definition for function Economic ( ) in C++ to read each record of a binary file ITEMS.DAT, find and display those items, which costs less than 2500. Assume that the file ITEMS.DAT is created with the help of objects of class ITEMS, which is defined below :

class ITEMS
{
int ID;char GIFT[20]; float Cost;
public :
void Get()
{
cin>>CODE;gets(GIFT);cin>>Cost;
}
void See()
{
cout<<ID<<”:”<<GIFT<<”:”<<Cost<<end1;
}
float GetCost(){return Cost;}.
};

7) Find the output of the following C++ code considering that the binary file
        CLIENTS.DAT exists on the hard disk with records of 100 members.

class CLIENTS
{
int Cno;char Name[20];
public :
void In(); void Out();
};
void main()
{
fstream CF;
CF.open(“CLIENTS.DAT”,ios::binary|ios::in);
CLIENTS C;
CF.read((char*) &C, sizeof(C));
CF.read((char*) &C, sizeof(C));
CF.read((char*) &C, sizeof(C));
int POS=CF.tellg()/sizeof(C);
cout<<”PRESENT RECORD:”<<POS<<end1;
CF.close();
}




8) Fill in the blanks marked as Statement 1 and Statement 2, in the program segment given below with appropriate functions for the required task.  
class Agency
{
int ANo; //Agent Code
char AName[20]; //Agent Name
char Mobile[12]; //Agent Mobile
public:
void Enter(); //Function to enter details of agent
void Disp(); //Function to display details of agent
int RAno(){return ANo;}
void UpdateMobile() //Function to update Mobile
{
cout<<²Updated Mobile:²;
gets(Mobile);
}
};
void AgentUpdate()
{
fstream F;
F.open(“AGENT.DAT²,ios::binary|ios::in|ios::out);
int Updt=0;
int UAno;
cout<<²Ano (Agent No - to update Mobile):²;
cin>>UAno;
Agency A;
while (!Updt && F.read((char*)&A,sizeof(A)))
{
if (A.RAno()==UAno)
{
//Statement 1:To call the function to Update Mobile No.
_______________________________________ ;

//Statement 2:To reposition file pointer to re-write
the updated object back in the file
_________________________________________ ;
F.write((char*)&A,sizeof(A));
Updt++;
}
}
if (Updt)
cout<<²Mobile Updated for Agent²<<UAno<<endl;
else
cout<<²Agent not in the Agency²<<endl;
F.close();
}
9) Assuming the class TOYS as declared below, write a function in C++ to read the objects of TOYS from binary file TOYS.DAT and display those details of those TOYS, which are meant for children of AgeRange ‘‘5 to 8’’.  
class TOYS
{
int ToyCode;
char ToyName[10];
char AgeRange;
public:
void Enter()
{
cin>>ToyCode;
gets(ToyName);
gets(AgeRange);
}
void Display()
{
cout<<ToyCode<<²:²<<ToyName<<endl;
cout<<AgeRange<<endl;
}
char* WhatAge(){return AgeRange;}
};

10) Given the binary file CAR.Dat, containing records of the following class CAR type:   
class CAR
{
int C_No;
char C_Name[20];
float Milage;
public:
void enter( ) {
cin>> C_No ; gets(C_Name) ; cin >> Milage;
}
void display( )
{
cout<< C_No ; cout<<C_Name ; cout<< Milage;
}
int RETURN_Milage( )
{
return Milage; } };
Write a function in C++, that would read contents from the file CAR.DAT and display the details of car with mileage between 100 to 150.

10) Given the binary file SBAccount.dat, containing records of the following class ACC type: 
class ACC
{
int AccNo;
char Name[20];
double Balanceamt;
public:
void enter( ) {  cin>> AccNo; gets(Name) ; cin >> Balance;  }
void display( )
{  cout<< AccNo; cout<< Name ; cout<< Balance;  }
double RETURN_Balance( )
{  return Balance; }
};
Write a function in C++, that would read contents from the file SBAccount.DAT and write the record contents of those ,whose  Balanceamt  is more than 500000 into another binary file “Privilege.dat” .  

BOARD PATTERN QUESTIONS (TEXT FILES)


 1)    Write a program which uses a function CHANGE() that reads a text file “IN.TXT” and write the contents from this file on to another file called “OUT.TXT” with the vowels capitalized.
If “IN.TXT” contains
A creative man is motivated by the desire to achieve, not by the desire to beat others. 
 
“OUT.TXT” contains
A crEAtIvE mAn Is mOtIvAtEd by thE dEsIrE tO AchIEvE, nOt by thE dEsIrE tO bEAt OthErs. 

2)    Write function definition for WORD4CHAR() in C++ to read the content of a text
file FUN.TXT, and display all those words, which has four characters in it.  

Example :

If the content of the file fun.TXT is as follows :
When I was a small child, I used to play in the garden with my grand mom. Those days were amazingly funful and I remember all the moments of that time

The function WORD4CHAR() should display the following :
When used play with days were that time

3)    Write a function AECount() in C++, which should read each character of a text file NOTES.TXT, should count and display the occurrence of alphabets A and E (including small cases a and e too).  
Example :
If the file content is as follows :
CBSE enhanced its CCE guidelines further.

The AECount() function should display the output as
A:1
E:7

4)    Write the command to place the file pointer at the 10th and 4th record starting position using seekp() or seekg() command. File object is ‘file’ and record name is ‘STUDENT’.

5)     Assume a input file stream “FILE” is opened in ios::in mode and is associated with the file “comp.dat”, that contains records of the following structure TEST.  Write a statement in C++ to position the file pointer just before the last record

struct TEST { int RegNo, mark;}obj;

6)     Write a function to count no. of character spaces and number of alphabets in a text file “test.dat”

1)    Write a program which uses a function CHANGE() that reads a text file “IN.TXT” and write the contents from this file on to another file called “OUT.TXT” with the vowels capitalized.
If “IN.TXT” contains
A creative man is motivated by the desire to achieve, not by the desire to beat others. 
 
“OUT.TXT” contains
A crEAtIvE mAn Is mOtIvAtEd by thE dEsIrE tO AchIEvE, nOt by thE dEsIrE tO bEAt OthErs. 

2)    Write function definition for WORD4CHAR() in C++ to read the content of a text
file FUN.TXT, and display all those words, which has four characters in it.  

Example :

If the content of the file fun.TXT is as follows :
When I was a small child, I used to play in the garden with my grand mom. Those days were amazingly funful and I remember all the moments of that time

The function WORD4CHAR() should display the following :
When used play with days were that time

3)    Write a function AECount() in C++, which should read each character of a text file NOTES.TXT, should count and display the occurrence of alphabets A and E (including small cases a and e too).  
Example :
If the file content is as follows :
CBSE enhanced its CCE guidelines further.

The AECount() function should display the output as
A:1
E:7

4)    Write the command to place the file pointer at the 10th and 4th record starting position using seekp() or seekg() command. File object is ‘file’ and record name is ‘STUDENT’.

5)     Assume a input file stream “FILE” is opened in ios::in mode and is associated with the file “comp.dat”, that contains records of the following structure TEST.  Write a statement in C++ to position the file pointer just before the last record

struct TEST { int RegNo, mark;}obj;

6)     Write a function to count no. of character spaces and number of alphabets in a text file “test.dat”

 PRACTICAL BASED QUESTIONS (BINARY FILES)
EXERCISES IN BINARY FILES

1)    Create a binary file with objects of the class APPLE with the following specifications:
Product Name – string of 25 characters
Price – float
Public member functions:
void  Enter() – to enter details
void Display() – to display details.
float RET_PRICE() – returns the price

Write following non-member functions :
CREATE() – to call the member function Enter() and write the objects on to the binary file.
PRINT() – to read the file and  print the details of all the products the products which are priced between 30000 and 40000 (inclusive) from the file by calling the member  function Display() .


Enter the following data:
Product
Price
iPhone 6Plus
40999
iPhone SEGold
35399
iPhone 5S
30000
iPhone6
39300


3)Complete the definition of the following class.
class customer
{
int custid;
char custname[24];
float amount;
public:
void enter();// enter the customer details
void print();// print the customer details
int ret_custid();// return custid
};
Write a menu driven program which accepts an option from the user (1-Append , 2- Display, 3- Search, 4 – Exit) and implements these options through non-member functions Append(), Display(), Search() continuously as long as the user does not press the option 4.
·         Append() – will invoke the member function enter() and appends the records in the binary file customer.dat.
·         Display() – will read the records from the binary file customer.dat and invokes the print() function.
·         Search()-will input customer_id from the user and read the records from the binary file customer.dat , searches for the existence of the customer_id in the file, If found, invokes the print() function , otherwise displays the error message “Not found”.
·         Exit – returns to the program

4) Complete the definition of the following class.
class lab
{
int rollno;
int no_of_experiments;
float marks;
public:
void enter();// enter the lab details
void print();// print the lab details
void update(int x)
{marks = marks + x;}
int ret_no_of_exp();// return no_of_experiments
};


Write a menu driven program which accepts an option from the user (1-Append, 2-Modify, 3-Display, 4 – Exit) and implements these options through non-member functions Append(), Modify(), Display() continuously as long as the user does not press the option 4.
·         Append() – will invoke the member function enter() and appends the records in the binary file lab.dat.
·         Modify() – will read the records from the binary file lab.dat and  invokes the update() function that receives the additional marks as parameter. It adds 5 marks for those students whose no_of_experiments is 15 and above, otherwise 2 marks is added. ret_no_of_exp() function is invoked to return the no_of_experiments.
·         Display()- will read the records from the binary file lab.dat , prints all the records by invoking the print() function
·         Exit – returns to the program

5) Complete the definition of the following class.
class student
{
int rollno;
char name[25];
int Class,year;
char sec;
char admn_no[15];
public:
void enter();// enter the student details
void print();// print the student details
int ret_year();// return year
int ret_Class();// returns Class
};
    Write a menu driven program which accepts an option from the user (1-Append
2-Delete, 3-Display, 4 – Exit) and implements these options through non-member      functions Append(), Delete(), Display() continuously as long as the user does not press the option 4.
·         App () – will invoke the member function enter() and appends the records in the binary file student.dat.
·         Del() – will read the records from the binary file student.dat and deletes Class 12 student records belonging to the year 2015.
·         Disp()- will read the records from the  binary file student.dat , prints all the records by invoking the print() function
·         Exit – returns to the program

1 comment: