XII CS Material Chap7 2012 13
XII CS Material Chap7 2012 13
XII CS Material Chap7 2012 13
seekp(Pos-sizeof(C)) ;
OR
F.seekp(-sizeof(C), ios::cur);
#include <fstream.h>
class Client
{
long Cno;
charName[20],Email[30] ;
public:
Ans)
void COUNT ( )
{
ifstream Fil; // ifstreamFil(ARTICLE.TXT");
Fil. open(ARTICLE.TXT);
char Word[80] ,Ch;
int Cl =0, C2 = 0, I=O;
while(Fil.get(Ch))
{
if (Ch! = ' ')
Word[I++] = Ch;
else
{
Word[I] = \0;
if (strcmp (Word,this)==0)
Cl++;
else if (strcmp(Word,these)==0)
C2++;
I=0;
}
}
cout<<Count of -this- in file:"<<Cl;
cout<<Count of -these- in file:"<<C2;
// OR cout<<Count of -this- and
//these- in file: <<Cl+C2;
Fil.close( );
}
void Enter() ;
//Function to allow user to enter
//(modify) Email
void Modify() ;
long ReturnCno()
{
return Cno;
}
};
void ChangeEmail()
{
Client C;
fstream F;
F.open (INFO.DAT,ios::binary
|ios::in|ios::out);
long Cnoc; //Clients no. whose
//Email needs to be changed
cin>>Cnoc;
while (F.read((char*)&C,
sizeof(C)))
{
if (Cnoc= =C.ReturnCno())
{
C.Modify(); //Statement 1
int Pos = __________
class FLIGHT
{ int Fno;
//Flight Number
char From[20]; //Flight Starting Point
char To[20];
//Flight Destination
public:
char* GetFrom( )
{return From;}
char* GetTo( )
{return To;}
void Enter( )
{
cin>>Fno;
gets(From);
gets(To);
_______________ // Statement 2
//To move the file pointer to
//write the modified record
//back onto the file for the
//desired Cnoc
F.write((char*)&C, sizeof(C));
}
}
F.close();
}
Ans)
Statement 1:
F. tellg ( );
Statement 2:
Prepared by: Mr.M.Ravi Kiran
86
}
void Display( )
{
cout<<Fno<<:<<From<<:<<To<<endl;
}
}
};
void ChangeMobile( )
{
Customer C;
fstream F;
F.open(CONTACT.DAT,ios::binary|
ios::in|ios::out);
long Cnoc;
};
Ans)
void Read ( )
{
FLIGHT F;
ifstream fin;
fin.open(FLIGHT.DAT,ios::binary);
cin>>Cnoc;
while (F.read((char*)&C,sizeof(C)))
{
if (Choc==C.GetCno( ))
{
C.Modify( );
int Pos=_____________ //Statement 1
//To find the current position of file pointer
______________ //Statement 2
while(fin.read((char*)&F,sizeof(F)))
{
if (strcmp(F. GetTo(),Mumbai))
F.Display( ) ;
}
fin.close(); //
}
OR
void Read( )
{
FLIGHT F;
ifstream fin;
fin.open (FLIGHT. DAT, ios::binary) ;
F.write((char*)&C, sizeof(C));
}
}
F. close ( );
}
Ans)
if (fin)
{ fin.read((char*)&F, sizeof (F));
while(!fin.eof( ))
{
if (strcmp(F. GetTo( ),Mumbai))
F.Display( );
fin.read((char*)&F,sizeof(F))
}
fin.close( );
}
Statement 2
F.seekp(Pos-sizeof(C));
OR
File.seekp(-l*sizeof(C),ios::cur);
(b) Write a function in C++ to count the words
to and the present in a text file
POEM.TXT.
2
[Note that the words to and the are
complete words]
Ans)
void COUNT( )
{
ifstream Fil;
Fil. open (POEM.TXT);
void Enter( );
//Function to allow user to enter
void Modify( );
//Function to return value of Cno
long GetCno( )
{
return Cno;
Prepared by: Mr.M.Ravi Kiran
Statement 1
F.tellg( ) ;
87
}
}
cout<<Count of -to- in file:<<Cl;
cout<<Count of -the- in file:<<C2;
//OR coutCount of -to- and -the- in
Fil.close( );
}
void show( );
//Function to display the content
class TRAIN
{
int Tno;
// Train Number
charFrom[20];
// Train Starting Point
charTo [20];
// Train Destination
public:
char* GetFrom( )
{
return From;
}
char* GetTo( )
{
return To;
}
void Input( )
{
cin>>Tno;
gets(From);
gets(To);
}
void Show( )
{
cout<<Tno<<:<<From<<:<<To<<endl;
}
};
{
fstream File;
File.open(ITEMS.DAT, ios ::
binarylios ::inlios :: out);
int Pos = 1 ;
Stock S ;
while (Pos = = 1 && File.read((char*) &S,
sizeof (S)))
if (S. KnowIno( ) ==PINo)
{
S. Purchase (PQty);
//To update the number of Items
Ans)
void Read ( )
{
TRAIN T;
ifstream fin;
fin. open (TRAIN.DAT,ios::binary);
___________________;
//Line 2: To write the object S on to
//the binary file
}
if (Pos = = 1)
cout<<No updation done as
required Ino not found.. ;
File.close ( ) ;
}
while(fin.read((char*)&T, sizeof(T)))
{
if(strcmp(T.GetTo(),Delhi)==O)
T.Show( ) ;
}
fin.close( );
}
Ans)
Statement 2:
File.write((char*)&S, sizeof(S));
OR
File.write((char*)&S, sizeof(Stock));
DELHI 2009:
4. (a) Observe the program segment given
below carefully and fill in the blanks marked as
Prepared by: Mr.M.Ravi Kiran
Statement 1:
File.seekp(Pos);
OR
File.seekp(-sizeof(A), ios:: cur);
88
{
(b) Write a function COUNT_DO( ) in C++ to
count the presence of a word do in a text file
MEMO.TXT
2
return Status;
}
};
Example:
If the content of the file MEMO.TXT is as
follows:
I will do it, if you request me to do it.
It would have been done much earlier.
The function COUNT_DO ( ) will display the
following message:
Count of -do- in file : 2
Note: In the above example, do occurring as a
part of word done is not considered.
Ans)
void DisplayActive ( )
{
USER U;
ifstream fin;
fin.open (USER.DAT, ios::binary);
//OR ifstream fin (USER.DAT, ios::binary);
while(fin.read((char*)&U, sizeof(U)))
{
if(U.Getstatus()==A)
U.show( );
}
fin.close( );
}
OR
void DisplayActive()
{
USER U;
ifstream fin;
fin.open (USER.DAT, ios::binary);
Ans)
void COUNT_DO( )
{
ifstream Fi1;
Fil.open(MEMO.TXT);
//OR ifstream Fil(MEMO.TXT);
char Word[80],Ch;
int Count =0,I=0;
while(Fi1.get(Ch))
{
if (Ch! = )
Word[I++] = Ch;
else
{
Word[I] = \0;
if (strcmp (Word, do) ==0)
Count++;
I=O;
}
}
cout<<Count of-do- in file:<<Count;
Fil.close( );
}
if (fin)
{
fin.read((char*)&U, sizeof(U));
while(!fin.eof())
{
if (U.Getstatus( )==A)
U.show( ) ;
fin.read((char*)&U, sizeof(U))
}
fin.close( );
}
OUTSIDE DELHI 2009:
4. (a) Observe the program segment given
below carefully and fill the blanks marked as
Line 1 and Line 2 using fstream functions for
performing the required task. 1
#include <fstream.h>
class Library
{
long Ano;
class USER
{
int Uid ;
//User Id
char Uname [20];//User Name
char Status; //User Type: A Active I Inactive
char Title[20];
//Title - Title of the Book
int Qty;
Public:
void Register ( );
public:
void Enter (int);
void show ( ) ;
void Display();
char Getstatus ( )
Prepared by: Mr.M.Ravi Kiran
89
Fil.open(NOTES.TXT)
char Word[80],Ch;
int Count =0, I=0;
while(Fil.get(Ch))
{
if (Ch!= )
Word [I++] = Ch;
else
{
Word[I] = \0;
if (strcmp (Word, to) ==0)
Count++;
I=O;
}
}
Fil.close( );
cout<<Count of -to- in file: <<Count;
}
};
void BuyBook(long BANo,int BQty)
//BANo ->Ano of the book purchased
//BQty ->Number of books purchased
{
fstream File;
File.open(STOCK.DAT ,
ios::binary|ios::in|ios::out);
int position=-l;
Library L;
while(Position==-l &&
File.read((char*)&L,sizeof(L)))
if (L.GetAno()==BANo)
{
L.Buy(BQty);
//To update the number of Books
Position = File.tellg()-sizeof(L) ;
____________________;
//Line 1: To place the file pointer to the required position
____________________;
//Line 2:To write the object L on to the binary file
}
if (Position==-l)
cout<< No updation do:r{e as
required Ano not found..;
File.close( );
}
Ans)
Statement 1
File.seekp(Position);
OR
File. seekp (-sizeof (L), ios::cur);
public:
void Register( );
//Function to enter the content
Statement 2
File.write((char*)&L, sizeof(L));
OR
File.write((char*)&L,sizeof(Library));
void Display( );
//Function to display all data members
char WhatType( )
{
return Type;
}
};
Ans)
void DisplayL_M( )
{
CLUB C;
fstream fin;
fin. open (CLUB.DAT,
ios::binary|ios::in);
Example:
If the content of the file NOTES.TXT is as
follows:
It is very important to know that
smoking is injurious to health.
Let us take initiative to stop it.
The function COUNT_TO( ) will display the
following message:
Count of -to- in file: 3
Prepared by: Mr.M.Ravi Kiran
while(fin.read((char*)&C, sizeof(C))
{
90
if(C.WhatType()==L||C.WhatType()==M)
C .Display ( );
}
fin.close( );
}
OR
void Disp1ayL_M ( )
{
CLUB C;
fstream fin;
fin.open (CLUB.DAT, ios::binary | ios::in);
}
Record++ ;
}
if (Found = = 1)
cout << Record Updated ;
File.close( ) ;
}
Write the Statement1 to position the File Pointer
at the beginning of the Record for which the
Applicants Id matches with the argument
passed, and Statement 2 to write the updated
record at that position.
if(fin)
{
fin.read((char*)&C, sizeof(C));
while(!fin.eof())
{
if(C.WhatType()==L||C.WhatType()==M)
C. Display( );
fin.read((char*)&C, sizeof(C));
}
}
fin.close( );
}
Ans)
Statement 2
File.seekp(Record*sizeof(Applicant));
or
File.write((char*)&A,sizeof(A));
4.b) Write a function in C++ to count the
number of lowercase alphabets present in a text
file BOOK.TXT.
DELHI : 2008:
4.a)Observe the program segment given below
carefully, and answer the question that follows
Solution:
void LowerLetters( )
{ clrscr( );
ifstream fin("BOOK.TXT",ios::in);
char ch;
int lowercount=0;
while(fin)
{
fin.get(ch);
if(islower(ch))
lowercount++;
}
cout<<"\nTotal number of Lowercase
alphabets in the file = "<<lowercount;
getch( );
}
4.c)Given a binary file PHONE.DAT,
containing records of the following structure
type
class Applicant
{ long Aid ;
// Applicants Id
char Name[20] ; // Applicants Name
float Score ;
// Applicants Score
public ;
void Enroll( ) ;
void Disp( ) ;
void MarksScore( ) ;
//Function to change Score
long R_Aid( )
{
return Aid;
)
};
void ScoreUpdate (long Id)
{
fstream File ;
File.open (APPLI.DAT , ios :: binary |
ios :: in | ios :: out) ;
Applicant A ;
int Record = 0, Found = 0 ;
while (!Found && File.read
( (char*)&C, sizeof(c) ) )
{
if (Id = = A.R_Aid( ) )
{
cout << Enter new Score ;
A.MarksScore( ) ;
____________ //Statement 1
____________ //Statement 2
Found=1;
Prepared by: Mr.M.Ravi Kiran
Statement 1
File.seekp(File.tellp( )-sizeof(A));
class phonlist
{
char Name[20] ;
char Address[30] ;
char AreaCode[5] ;
char PhoneNo[15] ;
public ;
void Register( ) ;
void Show( ) ;
int CheckCode(char AC[ ])
{ return strcmp(AreaCode,AC) ;
}
};
91
if (Found = = 1)
cout << Record Updated ;
File.close( ) ;
}
Write the Statement to position the File Pointer
at the beginning of the Record for which the
Candidates Id matches with the argument
passed, and Statement 2 to write the updated
Record at that position.
Ans)
Statement 1
File.seekp(File.tellp( )-sizeof(C));
Or
File.seekp(Record*sizeof(C));
void TRANSFER( )
{
ifstream fin(PHONE.DAT,ios::in,ios::binary);
ofstream fout(PHONEBACK.DAT,
ios::out,ios::binary);
phonlist P;
while(fin)
// or while(!fin.eof( ))
{
fin.read((char*)&P,sizeof(P));
if(P.CheckCode(DEL)= = 0)
fout.write((char*)&P,sizeof(P));
}
fin.close( );
fout.close( );
}
Statement 2
File.write((char*)&C,sizeof(C));
Or
File.write((char*)&C,sizeof(Candidate));
4.b)Write a function in C++ to count the
number of uppercase alphabets present in a text
file ARTICLE.TXT.
Solution:
void UpperLetters( )
{ clrscr( );
ifstream fin("ARTICLE.TXT",ios::in);
char ch;
int uppercount=0;
while(fin)
{ fin.get(ch);
if(isupper(ch))
uppercount++;
}
cout<<"\nTotal number of Uppercase
alphabets in the file = "<<uppercount;
getch( );
}
4.c) Given a binary file TELEPHON.DAT,
containing records of the following class
Directory :
class candidate
{ long Cid ;
// Candidates Id
char CName[20]; // Candidates Name
float Marks ;
// Candidates Marks
public ;
void Enter( ) ;
void Display( ) ;
void MarksChange( ) ;
//Function to change marks
long R_Cid( )
{
return Cid;
}
};
void MarksUpdate (long Id)
{
fstream File ;
File.open (CANDIDATE.DAT, ios ::
binary|ios::in|ios :: out) ;
Candidate C ;
int Record = 0, Found = 0 ;
while (!Found&&File.read((char*)&C,
sizeof(C)))
{
if (Id = =C.R_Cid( ))
{ cout << Enter new Marks ;
C.MarksChange( ) ;
____________ //Statement1
___________ //Statement 2
Found = 1 ;
}
Record++ ;
}
Prepared by: Mr.M.Ravi Kiran
class Directory
{ char Name[20] ;
char Address[30] ;
char AreaCode[5] ;
char phone_No[15] ;
public ;
void Register( ) ;
void Show( ) ;
int CheckCode(char AC[ ])
{
return strcmp(AreaCode, AC) ;
}
};
Write a function COPYABC( ) in C++, that
would copy all those records having AreaCode
as
123
from
TELEPHON.DAT
to
TELEBACK.DAT.
92
Solution:
void COPYABC( )
{ ifstream fin(TELEPHON.DAT,ios::in|
ios::binary);
ofstream fout(TELEBACK.DAT,ios::out,ios|
binary);
Directory D;
while(fin) // or while(!fin.eof( ))
{
fin.read((char*)&D,sizeof(D));
if(D.CheckCode(123)= = 0)
fout.write((char*)&D,sizeof(D));
}
fin.close( );
fout.close( );
}
DELHI : 2007
4.a) Observe the program segment given below
carefully, and answer the question that follows
class PracFile
{ int Pracno ;
char PracName[20]
int TimeTaken ;
int Marks ;
public :
void EnterPrac( ) ;
Solution:
Ans)
void ShowPrac( ) :
//Function to display PracFile details
void COUNT_IS ( )
{ ifstream Fil;
//OR ifstream Fi1(NOTES.TXT);
Fil.open(DIALOGUE.TXT)
char Word[80],Ch;
int Count =0, I=0;
while(Fil.get(Ch))
{
if (Ch!= )
Word [I++] = Ch;
else
{
Word[I] = \0;
if (strcmp (Word, is) ==0)
Count++;
I=O;
}
}
Fil.close( );
cout<<Count of -is- in file: <<Count;
}
{
Marks = M ;
}
};
void AllocateMarks( )
{
fstream File ;
File.open (MARKS.DAT, ios :: binary
| ios :: in | ios :: out ) ;
PracFile P ;
int Record = 0 ;
while (File.read ( (char*) &P, sizeof (P) ) )
{
if (P.RTime( ) > 50)
P.Assignmarks(0)
else
P.Assignmarks(10)
_____________;//Statement 1
_____________;//Statement2
Record++ ;
}
File . close( ) ;
}
Prepared by: Mr.M.Ravi Kiran
93
int Rec=0 ;
while (File.read ( (char*) &L,sizeof (L)))
{
if (L.RChecked( )= =N)
L.Assignmarks(0)
else
L.Assignmarks (10)
_______________; //Statement 1
Solution:
void BPlayers( )
{
ifstream fin(GAME.DAT,ios::in,ios::binary);
ofstream fout(BASKET.DAT,ios::out|
ios::binary);
Game G;
while(fin) // or while(!fin.eof( ))
{
fin.read((char*)&G,sizeof(Game));
if(strcmp(G.GameName,Basket Ball)= = 0)
fout.write((char*)&G,sizeof(G));
}
fin.close( );
fout.close( );
}
________________;//Statement 2
Rec++ ;
}
File.close( ) ;
}
If the function ModifyMarks ( ) is supposed to
modify marks for the records in the file
MARKS.DAT based on their status of the
member Checked (containg value either Y or
N).Write C++ statements for the statement 1
and statement 2,where, statement 1 is required
to position the file write pointer to an
appropriate place in the file and statement 2 is to
perform the write operation with the modified
record.
Ans)
Statement 1
File.seekp(File.tellp( )-sizeof(L));
or
File.seekp(Rec*sizeof(L));
Statement 2
File.write((char*)&L,sizeof(L));
or
File.write((char*)&L,sizeof(Labrecord));
class Labrecord
{ int Expno;
char Experiment[20] ;
char Checked ;
int Marks ;
public :
void EnterExp( ) ;
//function to enter Experiment details
viod ShowExp( ) ;
//function to display Experiment details
char RChecked( )
//function to return Expno
{
return Checked;
}
void Assignmarks (int M)
//function to assign Marks
{
Marks = M;
}
};
void ModifyMarks( )
{
fstream File ;
File.open (Marks.Dat, ios :: binary l
ios :: in l ios :: out) ;
Labrecord L ;
Prepared by: Mr.M.Ravi Kiran
94
Word[I] = \0;
if (strcmp (strupr(Word), THE) ==0)
Count++;
I=O;
}
}
Fil.close( );
cout<<Count of-the- in file: <<Count;
}
Solution:
void WordsCount( )
{ clrscr( );
ifstream fin("PARA.TXT",ios::in);
char ch;
int Words=1;
if(!fin)
{ cout<<No words at all in the file;
exit(0);
}
struct Sports
{
char Event[20] ;
char Participant[10][30] ;
};
Write a function in C++ that would read
contents from the file SPORTS.DAT and
creates a file named ATHLETIC.DAT copying
only those records from SPORTS.DAT where
the event name is Athletics.
Solution:
void AthletsList( )
{
ifstream fin(SPORTS.DAT,ios::in,
ios::binary););
ofstream fout(ATHLETIC.DAT,
ios::out|ios::binary);
Sports S;
while(fin)
// or while(!fin.eof( ))
{
fin.read((char*)&S,sizeof(Sports));
if(strcmp(S.Event,Athletics)= = 0)
fout.write((char*)&S,sizeof(S));
}
fin.close( );
fout.close( );
}
while(fin)
{ fin.get(ch);
if(ch= = )
Words++;
}
cout<<"\nTotal number of Words in
the file = "<<Words;
getch( );
}
4.c)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 ;
};
DELHI : 2006
4.a)
void main( )
{
char ch = A ;
fstream fileout(data.dat, ios::out) ;
fileout<<ch ;
int p = fileout.tellg( )
cout<<p ;
}
What is the output if the file content before the
execution of the program is
the string ABC
(Note that are not part of the file).
Ans) 1
Prepared by: Mr.M.Ravi Kiran
Solution:
void Update( )
{
fstream finout(COLONY.DAT,ios::in|
ios::out);
COLONY C;
finout.seekg(0);
while(finout)
{finout.read((char *)&C, sizeof(C));
cout<<\nThe Colony Code is
<<C.Colony_Code;
95
}
OUTSIDE DELHI : 2006
4.a)
void main( )
{
char ch = A ;
fstream fileout(data.dat, ios :: app);
fileout<<ch ;
int p = fileout.tellg( ) ;
cout << p ;
}
What is the output if the file content before the
execution of the program is the string ? ABC
(Note that are not part of the file)
Solution:
void Update( )
{
fstream finout(PRODUCT.DAT,
ios::in|ios::out);
PRODUCT P;
finout.seekg(0);
while(finout)
{
finout.read((char *)&P, sizeof(P));
cout<<\nThe Product Code is
<<P.Product_Code;
cout<<\nThe Product
Description is<<
P.Product_Description;
cout<<\nEnter the Stock: ;
cin>>P.Stock;
finout.seekp(finout.seekp( )-sizeof(P));
finout.write((char *)&P,sizeof(P));
}
}
Solution:
DELHI : 2005
void BlanksCount( )
{
clrscr( );
ifstream fin("PARA.TXT",ios::in);
char ch;
int Blanks=0;
if(!fin)
{ cout<<No words at all in the file.
So no blank spaces;
exit(0);
}
while(fin)
{
fin.get(ch);
if(ch= = )
Blanks++;
}
cout<<"\nTotal number of Blank
Spaces in the file = "<<Blanks;
getch( );
}
Ans)4
(Since, the file is opened in app mode, it retains
the previous content also, if the file mode is out,
then result will be 0 since it will loose all the
old content of the file.)
class Book
{ int Book_no :
char Book_name[20] ;
public ;
void enterdetails( ) ;
//function to enter Book details
void showdetails( ) ;
//function to display Book details
int Rbook_no( )
{return Book_no ;
}
//function to return Book_no
};
void Modify (Book NEW)
{ fstream File ;
File.open(BOOK.DAT, ios :: binary
|ios :: in | ios :: out) ;
Book OB ;
96
}
If the function Modify( ) is supposed to modify
a record in file BOOK.DAT with the values of
Book NEW passed to its argument, write the
appropriate statement for Missing Statement
using seekp( ) or seekg( ), whichever needed, in
the above code that would write the modified
record at its proper place.
Ans)
File.seekp((Recordsread-1)*sizeof(NEW));
OR
File.seekp(-sizeof(NEW), ios::curr);
OR
File.seekp(File.tellg()-sizeof(NEW));
Ans)
void Distinction()
{
Student S;
fstream Fin;
Fin.open(STUDENT.DAT,
ios::binary|ios::in);
while(Fin.read((char*)&S,
sizeof(Student))
if (S.ReturnPercentage()>75)
S.DisplayData();
Fin.close();
}
Example :
If the file LINES.TXT contains the following
lines,
A boy is playing there.
There is a playground.
An aeroplane is in the sky.
Alphabets and numbers are allowed
in the password.
The function should display the output as 3
Ans)
void counter( )
{ char Aline[80];
int Count=0;
ifstream Fin (LINES.TXT);
while(Fin.getline(Aline,80, \n))
if (Aline[0]== A)
Count++;
Fin.close( );
cout<<Count<<endl;
}
Prepared by: Mr.M.Ravi Kiran
public :
void enterdetails ( ) ;
//function to enter Member details
void showdetails ( ) ;
//function to display Member details
int RMember_no( )
{
return Member_no;
} //function to return Member_no
};
alphabet A present
STORY.TXT.
text
Ans)
void COUNTALINES()
{
ifstream FILE(STORY.TXT);
int CA=0;
char LINE[80];
while (FILE.getline (LINE,80))
if (LINE[0]!=A)
CA++;
cout<<Not Starting with A counts to
<<CA<<endl;
FILE.close( );
}
4.c) Given a binary file APPLY.DAT,
containing records of the following class
Applicant type
3
class Applicant
{ char A_Rno[10] ;
//Roll number of applicant
char A_Name[30];
//Name of applicant
int A_Score; //Score of applicant
public :
void Enrol( )
{ gets(A_Rno);
gets(A_Name);
cin >> A_Score ;
}
void Status( )
{
cout << setw(12) << A_Admno ;
cout << setw(32) << A_Name ;
cout << setw(3) << A_Score << endl ;
}
int ReturnScore( )
{
return A_Score;
}
};
Write a function in C++, that would read
contents of file APPLY.DAT and display the
details of those Students whose A_Score is
above 70.
Example :
If the file STORY.TXT contains the
following lines,
The rose is red.
A girl is playing there.
There is a playground.
An aeroplane is in the sky.
Numbers are not allowed in the
password.
The function should display the output as 3
in
98
file
DELHI : 2004
void Showonscreen( )
{
cout<<Idno<<:<<Type<<endl
<<Desc<<endl;
}
};
DELHI : 2002
4.a)What is the difference between pub( ) and
write( ) ?
DELHI : 2001
4.a) Distinguish
ios::app.
and
DELHI : 2003
4.a) Write a user defined function in C++ to
read the content from a text file NOTES.TXT,
count and display the number of blank spaces
present in it.
4.b)Assuming a binary file FUN.DAT is
containing objects belonging to a class
LAUGHTER (as defined below).Write a user
defined function in C++ to add more objects
belonging to class LAUGHTER at the bottom of
it.
2000:
Q 4 (a) Name two member functions of
ofstream class
class LAUGHTER
{ int Idno;
// Identification number
char Type[5]; //LAUGHTER Type
char Desc[255];//Description
public :
void Newentry( )
{ cin>>Idno;gets(Type);gets(Desc);
}
XII Computer Chap - 6
ios::out
between
class DRINKS
{ int DCODE;
char DNAME[13]; //Name of the drink
int DSIZE,;
//Size in liters
float DPRICE;
public:
void getdrinks( )
{
cin>>DCODE>>DNAME>
>DSIZE>>DPRICE;
}
void showdrinks( )
{
cout<<DCODE<<DNAME<<DSIZE<<
DPRICE<<endl;
}
char *getname( )
{return DNAME;}
};
char ENAME[10];
public:
void GETIT( )
{
cin>>ENO;
gets(ENAME);
}
void SHOWIT( )
{ cout< < ENO<<ENAME<<endl;
}
};
1999:
#include <fstream.h>
class Item
{
int Ino;
char Item[20];
public:
void Search(int );
//Function to search and display the
//content from a particular record number
void Modify(int);
//Function to modify the content of a
//particular record number
};
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( );
}
Ans)
Statement 2
File.seekp(RecNo*sizeof(Item));
class EMPLOYEE
{
int ENO;
Prepared by: Mr.M.Ravi Kiran
Statement 1
File.seekg(RecNo*sizeof(Item));
100
Ans)
void CountLine()
{
ifstream FIL("STORY.TXT");
int LINES=0;
char STR[80];
while (FIL.getline(STR,80))
LINES++;
cout<<"No. of Lines:"<<LINES<<endl;
f.close( );
}
#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;
}
Ans)
Statement 1
File.seekg(0,ios::end);
Statement 2
File.tellg( );
b) Write a function in C++ to count the number
of alphabets present in a text file
"NOTES.TXT".
2
Ans)
void CountAlphabet()
{
ifstream FIL("NOTES.TXT");
int CALPHA=0;
char CH=FIL.get();
while (!FIL.eof())
{
if (isalpha(CH))
CALPHA++;
CH=FIL.get( );
}
cout<<"No. of Alphabets:"<<
CALPHA<<endl;
}
Ans)
void BookSearch()
{
fstream FIL;
FIL.open("BOOK.DAT",ios::binary|ios::in);
BOOK B;
int bn,Found=0;
cout<<"Enter Book No. to search"; cin>>bn;
while (FIL.read((char*)&S,sizeof(S)))
if (FIL.RBno()==bn)
{
S.Display();
Found++;
}
if (Found==0)
cout<<"Sorry! Book not
found!!!"<<endl;
FIL.close( );
}
101
void Enter( )
{ cin>>Rno;gets(Name);
}
void Display( )
{ cout<<Rno<<Name<<endl;
::in|ios::out);
cout>>Ino;
cin.getline(Item,20);
_________________ //Statement 2
File.write((char*)this,sizeof(Item));
File.close( );
}
Answer:
Statement 1
File.seekg(RecNo*sizeof(Item));
}
};
Ans)
void Addnew( )
{
fstream FIL;
FIL.open("STUDENT.DAT",ios::binary|
ios::app);
STUD S;
char CH;
do
{
S.Enter();
FIL.write((char*)&S,sizeof(S));
cout<<"More(Y/N)?";cin>>CH;
}
while(CH!='Y');
FIL.close( );
}
Statement 2
File.seekp(RecNo*sizeof(Item));
(b) Write a function in C++ to count the number
of lines present in a text file STORY.TXT. 2
Answer:
void CountLine( )
{
ifstream FIL(STORY.TXT);
int LINES=0;
char STR[80];
while (FIL.getline(STR,80))
LINES++;
cout<<No. of Lines:<<LINES<<endl;
FIL.close( );
}
#include <fstream.h>
class Item
{
int Ino;char Item[20];
public:
void Search(int );
//Function to search and display the content
//from a particular record number
void Modify(int);
//Function to modify the content of a
//particular record number
class BOOK
{int Bno;
char Title[20];
public:
int RBno( )
{ return Bno;
}
void Enter( )
{ cin>>Bno;gets(Title);
}
void Display( )
{
cout<<Bno<<Title<<endl;
}
};
Answer:
void BookSearch()
{
fstream FIL;
FIL.open(BOOK.DAT,ios::binary|ios::in);
BOOK B;
int bn,Found=0;
cout<<Enter Book Num to search;
cin>>bn;
};
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
Prepared by: Mr.M.Ravi Kiran
102
while (FIL.read((char*)&S,sizeof(S)))
if (B.RBno( )==bn)
{
B.Display( );
Found++;
}
if (Found==0) cout<<Sorry! Book
not found!!!<<endl;
FIL.close( );
}
2008-09 MODEL PAPER 2:
4.(a) 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.
1
#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;
}
Answer:
File.seekg(0,ios::end);
//Statement 1
File.tellg( );
//Statement 2
103
{ if (Id = =C.R_Cid( ))
{ cout << Enter new Marks ;
C.MarksChange( ) ;
____________ //Statement1
___________ //Statement 2
Found = 1 ;
}
Record++ ;
}
if (Found = = 1)
cout << Record Updated ;
File.close( ) ;
}
Write the Statement to position the File Pointer
at the beginning of the Record for which the
Candidates Id matches with the argument
passed, and Statement 2 to write the updated
Record at that position.
Ans)
Statement 1
File.seekp(File.tellp( )-sizeof(C));
Or
File.seekp(Record*sizeof(C));
Statement 2
File.write((char*)&C,sizeof(C));
Or
File.write((char*)&C,sizeof(Candidate
));
2.Write a function in C++ to count the words
this and these present in a text file
ARTICLE.TXT.
[Note that the words this and these are
complete words]
Ans)
void COUNT ( )
{ifstream Fil;
// ifstreamFil(ARTICLE.TXT");
Fil. open(ARTICLE.TXT);
char Word[80] ,Ch;
int Cl =0, C2 = 0, I=O;
while(Fil.get(Ch))
{ if (Ch! = ' ')
Word[I++] = Ch;
else
{ Word[I] = \0;
if (strcmp (Word,this)==0)
Cl++;
else if (strcmp(Word,these)==0)
C2++;
I=0;
}
}
cout<<Count of -this- in file:"<<Cl;
cout<<Count of -these- in file:"<<C2;
Fil.close( );
}
TEST
1.Observe the program segment given below
carefully and fill the blanks marked as
Statement 1 and Statement 2 using tellg() and
seekp() functions for performing the required
task.
#include <fstream.h>
class Customer
{ long Cno;
char Name[20],Mobile[12];
public:
//Function to allow user to enter the Cno,
Name,Mobile
void Enter( );
//Function to allow user to enter (modify)
mobile number
void Modify( );
//Function to return value of Cno
long GetCno( )
{
return Cno;
}
};
void ChangeMobile( )
{Customer C;
fstream F;
F.open(CONTACT.DAT,ios::binary|
ios::in|ios::out);
long Cnoc;
//Customer no. whose mobile number needs to
be changed
cin>>Cnoc;
while (F.read((char*)&C,sizeof(C)))
{
if (Choc==C.GetCno( ))
{
C.Modify( );
int Pos=_____________ //Statement 1
//To find the current position of file
pointer
______________ //Statement 2
//To move the file pointer to write the
modified record
//back onto the file for the desired Cnoc
F.write((char*)&C, sizeof(C));
}
}
F. close ( ) ;
}
Solution:
void COPYABC( )
{ ifstream fin(TELEPHON.DAT,ios::in|
ios::binary);
ofstream fout(TELEBACK.DAT,ios::out,ios|
binary);
Directory D;
while(fin) // or while(!fin.eof( ))
{ fin.read((char*)&D,sizeof(D));
if(D.CheckCode(123)= = 0)
fout.write((char*)&D,sizeof(D));
}
fin.close( );
fout.close( );
}
105
106