DATA-FILE-HANDLING-QUESTION-BANK-XII-CS
|
|
Q1. Write
a function in C++ to count and display the number of line starting with
alphabet ‘A’ present in a text file “LINES.TXT”.
Example:
if the file LINES.TXT contains the following lines:
A boy is
playing there.
There is
a playground.
An
airplane is in the sky.
Alphabets
and numbers are allowed in the password.
Then the
function should display the output as 3.
………………ANSWER…………………………..
#include<fstream.h>
#include<conio.h>
void main()
{
ifstream
file("LINES.txt",ios::in);
if(!file) { cout<<" ERROR in
OPENING FILE. "; return;}
char oneline[80]; int nol=0;
while(file)
{ file.getline(oneline,80);
if(oneline[0]=='A') nol++;
}
cout<<"\n NUMBER OF LINES IN
THE SPECIFIED TEXT FILE = "<<nol;
file.close();
getch();
}
…………………………………………………….
|
|
|
Q2.Write a function in C++ to count the number
of lines and words present in a text file “san.txt”.
………………………ANSWER………………………………
#include<fstream.h>
#include<conio.h>
void
main()
{
ifstream file("san.txt",ios::in);
if(!file) { cout<<" ERROR in
OPENING FILE. "; return;}
char oneline[80]; int nol=0;
while(file)
{ file.getline(oneline,80);
nol++;
}
nol--;
cout<<"\n TOAL NUMBER OF
LINES IN THE SPECIFIED TEXT FILE = "<<nol;
file.close();
file.open("san.txt",ios::in);
if(!file) { cout<<" ERROR in
OPENING FILE. "; return;}
char oneword[80]; int now=0;
while(file)
{ file>>oneword;
now++;
}
now--;
cout<<"\n TOAL NUMBER OF
WORDS IN THE SPECIFIED TEXT FILE = "<<now;
file.close();
getch();
}
……………………………………………………………………
Q3.Write
function to count the number of digits present in the given text file.
……………………………………………………………………
#include<fstream.h>
#include<conio.h>
void
main()
{
ifstream
file("SAMPLE.txt",ios::in);
if(!file) { cout<<" ERROR in
OPENING FILE. "; return;}
char c; int nod=0;
while(file)
{ file.get(c);
if(c>'0' && c< '9')
nod++;
}
cout<<"\n TOAL NUMBER OF DIGITS
IN THE SPECIFIED TEXT FILE = "<<nod;
file.close();
getch();
}
…………………………………………………………………………
Q4.Write C++ function display_male() that
displays the details of all the Male members of the binary file “society.dat”
containing the details from the following class:
class Society {
int memid;
char memname[20];
char gender; // ‘M’ or ‘F’
public:
void setdata();
void display();
char getgender(){ return gender;}
};
………………………………………………………………………………………………………………………………
A COMPLETE
PROGRAM TO CREATE A
NEW FILE AND
DISPLAY CONTENT OF
IT
……………………………………................................................................................................
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
class
society {
int memid;
char memname[20];
char gender; // 'M' or 'F'
public:
void setdata();
void display();
char getgender(){ return
gender;}
};
void
society::setdata()
{
cout<<"\n Please enter member
id : "; cin>>memid; ;
cout<<"\n Please enter member
name :"; gets(memname);
cout<<"\n Please enter
gender : M/F ";
cin>>gender;
}
void
society::display()
{
cout<<"\n Member id : ";
cout<<memid;
cout<<"\n Member name :";
cout<<memname;
cout<<"\n Gender
: M/F "; cout<<gender;//vvv
}
void
main()
{
society soc;
fstream
file("data",ios::out|ios::binary);
//create a new file "data"
if(!file) { cout<<" ERROR in
OPENING FILE. "; return;}
int i=1, n;
cout<<"\n How many records you want to enter in specified Binary file
:";
cin>>n;
while(i<=n)
{ soc.setdata();
file.write((char
*)&soc,sizeof(soc));
i++;
}
file.close();
file.open("data",ios::in|ios::binary); //open file in read
only mode
cout<<"\n CONTENT OF BINARY
FILE (INCLUDING BOTH MALE AND FEMALE ) : \n ";
i=1;
while(i<=n)
{
file.read((char
*)&soc,sizeof(soc));
soc.display();
i++;
}
cout<<"\n details of only
Male members are : \n ";
file.seekg(0,ios::beg);
while(file.read((char
*)&soc,sizeof(soc)))
{
if (soc.getgender()=='M'
||soc.getgender()=='m')
soc.display();
}
file.close();
getch();
}
…………………………………………………………………………………………………………………………..
function
display_male() that displays the details of all the Male members of the
binary file “society.dat” containing the details from the following class
…………………………………………
ANSWER………………………..
Void
display_male() {
ifstream
f("data",ios::in|ios::binary); //open file in read only mode;
cout<<"\n details of only
Male members are : \n ";
while(f.read((char
*)&soc,sizeof(soc)))
{
if (soc.getgender()=='M'
||soc.getgender()=='m')
soc.display();
}
file.close(); }
Q5.Assuming the class ZOO given below, write
functions in C++ to do following:
i.
Write the
objects of ZOO to a binary file.
ii.
Read the
objects of ZOO from binary file and display them on screen.
class
ZOO { char location[20];
int
no_of_animals[10];
public:
void readdata()
{cin>>location;
gets(no_of_animals);
}
void
writedata()
{ cout<<location<<” ”<<no_of_animals<<endl;}
};
…………………………………………………………….
void
Write2File(ZOO z)
{
ofstream
file("data",ios::out|ios::binary);
//create a new file "data"
if(!file) { cout<<" ERROR in
OPENING FILE. "; return;}
int i=1, n;
cout<<"\n How many records you want to enter in specified Binary file
:";
cin>>n;
while(i<=n)
{ z.readdata();
file.write((char *)&z,sizeof(z));
i++;
}
file.close();
}
void
ReadFromFile()
{
ifstream
file("data",ios::in|ios::binary);
//read from file
"data"
if(!file) { cout<<" ERROR in
OPENING FILE. "; return;}
while(file.read((char *)&z,sizeof(z)))
{ z.writedata(); }
file.close();
}
Q6.Following
is the structure of each record in a data file named “Colony.dat”
struct
Colony {
char colony_code[10];
char colony_name[10];
int no_of_people; };
Wite a
function to update the file with a new value of No_of_people. The value of
colony_code and no_of_people are read
during the execution of the program.
…………………………………………… ANSWER ……………………………………………………
void
UpdateFile()
{
Colony col;
ifstream file("colony",ios::in|ios::binary); //read from
file "data"
if(!file) { cout<<" ERROR in
OPENING FILE. "; return;}
cout<<”\n Enter value of colony code : “;
c in>>col_code;
c out<<”\n Enter no_of_people : “;
cin>> No_of_people ;
while(file.read((char *)&col ,
sizeof(col)))
{
if(col.colony_code==col_code)
col.no_of_people
= No_of_people ;
}
file.close();
}
Q7.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<<endl;
}
};
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.
……………………………………………….ANSWER…………………………………………
void
readfile()
{
class
computer comp ;
int
count=0;
ifstream
file("SHIP.DAT",ios::in|ios::binary); //read from
file "data"
if(!file) { cout<<" ERROR in
OPENING FILE. "; return;}
cout<<”\n
CONTENT OF GIVEN BINARY FILE :\n”;
while(file.read((char *)&comp ,
sizeof(comp)))
{
comp.showdetails();
count++;
}
cout<<”\n
Total no of records = “<<count;
file.close();
}
|
|
|
Q8. Observe the following program segment and
fill in the blanks marked as statement 1 and statement 2
#include<fstream.h>
class Student{
int
id; char name[20], char hobby[20];
public:
void
entry();
void
modify();
int
getid(){ return id;}
};
void changehobby(int num)
{
Student me;
fstream file;
f.open(“info.dat”,ios::in|ios::out|ios::binary);
while(file.read((char *) &me, sizeof(me)))
{
if(num==me.getid())
{
me.modify();
long fp =……………………….. //
Statement 1 to acquire the
// position of the read
pointer.
……………………………………….. // Statement 2 to place the write
//pointer to the
proper position
//for writing.
file.write((char *) &me, sizeof(me))
}
}
file.close();
}
……………………………………………….ANSWER…………………………………………
fp = file.tellg(); //statement 1
file.seekp(fp-sizeof(me), ios :: beg); //
statement 2
|
Q9. Observe
the program segment given below carefully and fill the blanks marked statement
1 and statement 2 using seekg( ) and tellg( ) function for
performing the required task.
#include<fstream.h>
class
Employee
{int Eno;
char
Ename[30];
public:
int
Countrec( ); //Function to count
the total number of records
};
int
Employee:: Countrec( )
{
fstream
File;
File.open(“Emp.Dat”,ios::binary||ios::in);
___________
//
Statement 1
int Bytes =
________________ // Statement 2
int count =
Bytes/sizeof(Employee);
File.close(
);
return
count;
}
……………………………………………….ANSWER…………………………………………
File.seekg(0,ios::end); //statement 1
File.tellg(); // statement 2
Q10. Observe the program segment given below carefully, and answer the
question that follows:
class Examiner
{
int eno; /
/ examiner’s number
char
ename[20]; / / examiner’s
name
char
eadd[25]; / /
examiner’s address
int exp; / /
examiner’s experience
public:
void
read_data();
void
out_data();
void
revised(); // function to make
revisions in the experience
int
chno(); // returns the new
experience
};
void experience (int revno)
{fstream fin;
fin.open(“exam.dat”ios::binary|ios::in|ios::out);
Examiner ex;
int rec=0;got=0;
while(!got && fin.read((char*)&ex,sizeof(ex)))
{ if (revno=ex.chno())
{cout<<”enter the new experience”;
ex.revised();
___________________ /
/ statement 1
___________________ /
/ statement 2
got=1;
}
rec++; }
fin.close(); }
Write the statement 1 to position
the file pointer at the beginning of the record for which the examiner’s number matches with the argument passed, and
the statement 2 to write the updated
record at that position.
……………………………………………….ANSWER…………………………………………
fin.seekp(fin.tellg( ) – sizeof(i)); //statement 1
fin.write((char*)
&ex , sizeof (ex)); // statement 2
Q11. Observe the program segment
and answer the question that follows:
class item
{
int item_no; char item_name[20];
public:
void enterDetails( ); void showDetail( );
int getItem_no( ){ return item_no;}
};
void modify(item x ) {
fstream File;
File.open( “item.dat”,………………….. ) ; //parameter missing item i;
while(File .read((char*) & i ,
sizeof (i)))
{
if(x . getItem_no( ) = = i . getItem_no( ))
{
File.seekp(File.tellg( ) – sizeof(i));
File.write((char*)
&x , sizeof (x));
}
else
File.write((char*)
&i , sizeof (i));
}
File.close() ; }
If the function modify( ) modifies a record in the file “ item.dat
“ with the values of item x passed as argument,
write the appropriate parameter for the missing parameter in the above code, so as to
modify
record at its proper place.
………………………….
ANSWER………………
File.open(
“item.dat”,ios::ate|ios::binary)
Q12. A file named as “STUDENT.DAT” contains the student
records, i.e. objects of class student. Write the command to open the file to update a student
record.(Use suitable stream class and file mode(s).
…………………………………
ANSWER……………………………………..
fstream f(“STUDENT.DAT”,ios::ate|ios::binary)
Q13.Observe the program segment carefully and answer the question that
follows: (2M)
class member {
int member_no;
char member_name[20];
public:
void enterDetails( ); void showDetail( );
int getMember_no( ){ return member_no;}
};
void update(member NEW ){
fstream File;
File.open( “member.dat”,
ios::binary|ios::in|ios::out) ; member i;
while(File .read((char*)
& i , sizeof (i)))
{
if(NEW . getMember_no(
) = = i . getMember_no( ))
{ File.seekp(………………., ios::cur ) //Paremeter
Missing
File.write((char*) &NEW , sizeof (NEW));
}
} File.close() ;}
If the function update( ) is supposed to modify a record in the file “
member.dat” with the values of member NEW passed as argument, write the
appropriate parameter for the missing parameter in the above code, so as
to modify record at its proper place.
-------------------------------ANSWER ------------------------
File.seekp(
-sizeof(i) , ios::cur)
Q14. A file named as “STUDENT.DAT” contains
the student records, i.e. objects of class student. Assuming that the file is just opened through the object FILE of fstream class, in the required file mode, write the command to position the put pointer to point to second record from the last record.
…………………………. ANSWER……………………..
FILE.seekp(-2*sizeof(student)
, ios::end);
Q15. A file named as “STUDENT.DAT” contains the student
records, i.e. objects of class student. Assuming that the file is just opened through the object FILE of fstream class, in the required file mode, write the command to position
the get pointer to point to fifth record
from the beginning.
…………………………. ANSWER……………………..
FILE.seekg(4*sizeof(student)
, ios::beg);
Q16. Read the code given below and answer the question:
void main( )
{ char ch = ‘A’;
fstream outFile (“data.dat”, ios::out); outFile<<ch<<ch; }
If the file contains
GOOD before execution, what will be the contents
of the file after execution of this code?
…………………………. ANSWER……………………..
Content of file after execution of the
above code will be:
“AA”