C++ Set Question 4 (MM 6)
C++ Set Question 4 (MM 6)
File.close();
}
(i)Write statement 1 to position the file pointer to the beginning of the desired record to be read, which is sent
as parameter of the function (assuming RecNo1 stands for the first record)
(ii)Write statement 2 to position the file pointer to the beginning of the desired record to be modified, which is
sent as parameter of the function (assuming RecNo1 stands for the first record).
(b) Write a function in C++to count the word “this”(including “This”/”THIS” too) present in a text file
“DIARY.TXT”.
(C)Write a function in C++ to search for a Toy having a particular ToyCode from a binary file “TOY.DAT” and
display its details (Tdetails),assuming the binary file is containing the objects of the following class.
class TOYSHOP
{
intTcode; //Toy Code
charTdetails[20];
public:
intRTcode( )
{
1 | Page C++ Mo. No. 9810301034 [email protected]
Computer Science C++ By Gajendra Sir
return Tcode;
}
voidAddToy( )
{
cin>>Tcode;gets(Tdetails);
}
voidDisToy ( )
{
cout<<Tcode<<Tdetails<<endl;
}
};
Set 2 , Question 4 (MM: 6) [Data File]
Ques. 4.a) Observe the program segment given below carefully and fill the blanks marked statement 1 and
statement
class PracFile
{ int Pracno;
char PracName[20];
int TimeTaken;
int Marks;
public:
void EnterPrac( ); // function to enter PracFile details
void ShowPrac( ): // function to display PracFile details
int RTime() // function to return TimeTaken
{
return TimeTaken; }
void Assignmarks (int M) // function to assign Marks
{ 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
//statement 2
Record + + ;
}
File.close();
}
If the function AllocateMarks ( ) is supposed to Allocate Marks for the records in the file MARKS.DAT based
on their value of the member TimeTaken. 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.
b) Assume a text file “coordinate.txt” is already created. Using this file create a C++ function to count the
number of words having first character capital .Also count the presence of a word ‘Do’. Example:
2 | Page C++ Mo. No. 9810301034 [email protected]
Computer Science C++ By Gajendra Sir
Do less Thinking and pay more attention to your heart. Do Less Acquiring and pay more
Attention to what you already have. Do Less Complaining and pay more Attention to giving. Do Less criticizing
and pay more Attention to Complementing. Do less talking and pay more attention to SILENCE.
Output will be : Total words with capital vowel : 16
Count of ‘Do’ in file - 5
c) Given a binary file “BUS.DAT”, containing records of the following class bus type.
class bus
{
int bus_no;
char desc[40];
int distance; //in km public:
void read( )
{
cin>>bus_no; gets(desc) ;
cin>>distance; }
void display( )
{
cout<<bus_no;
puts(desc);
cout<<distance;
}
int retdist( )
{
return distance;
}
};
Write a function in C++ that would read the contents of file “BUS.DAT” and display the
details of those buses which travels the distance more than 100 km.
Set 3 , Question 4 (MM: 6) [Data File]
4. (a).Observer the program segment carefully and fill in the blanks marked as statement 1&2
#include<fstream.h>
class MATERIAL
{
public:
};
int Mno;char Mname[25]; int qty;
:
void ModifyQty();
void MATERAIL::ModifyQty()
{
Fstream File;
Fil.open("MATERIAL.DAT",ios::binary|ios::in|ios::out);
int Mpno;
cout<<"Materail no to modify Qty :";
cin>>Mpno;
while(Fil.read((char*)this,sizeof(MATERIAL)))
{
if(Mpno==Mno)
{
cout<<"Present Qty :" <<qty<,endl;
cout<"Changed Qty :";
cin>>qty;
3 | Page C++ Mo. No. 9810301034 [email protected]
Computer Science C++ By Gajendra Sir
int Position=_; //(Statement 1)
: //(Statement 2)
Fil.write((char * this,sizeof (MATERIAL)); // Re-writing the record
}
}
Fil.close();
}
(b) Write a function in C++ to print the count of the word as an independent word in a text file story.txt
Eg: There was a tiger in the zoo. The tiger was very naughty. The output of the program should be 2.
(c) Given a binary file Sports.dat, containing records of the following structure type:
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 “Atheletics”,
// Statement 1
// Statement 2
Record ++ ;
4 | Page C++ Mo. No. 9810301034 [email protected]
Computer Science C++ By Gajendra Sir
}
if ( found == 1 ) cout << “ Record Updated “ ;
File. close ( );
}
Write the Statement 1 to position the File pointer at the beginning of the Record for which the candidate’s Id
matches with the argument passed, and Statement 2 to write the updated Record at that position.
b) Write a function in C++ to count the number of uppercase alphabets present in a text file
“ ARTICLE.TXT”.
c) Given a binary file TELEPHON.DAT, containing records of the following class Directory :
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.
void FILE::GO_Record(int N)
{
c. Write a function in c++ to search and display details of all trains , whose destination is “Delhi” from a binary
file “TRAIN.DAT”. Assuming the binary file is containing the objects of the following class:
class TRAIN
{
int Tno; // Train Number
char From[20]; // Train Starting Point
char To[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;
}
};
Set 7, Question 4 (MM: 6) [Data File]
Q- 4 (a)Observe the program segment given below carefully and fill the blanks marked as Statement1 and
Statement2 using seekp( ) and seekg( ) functions for performing the required task.
#include <fstream.h>
class Item
{
int Imno; 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));
7 | Page C++ Mo. No. 9810301034 [email protected]
Computer Science C++ By Gajendra Sir
Cout <<Ino <<” = = >” << Item << endl;
File.close ( );
}
void Item :: Modify (int RecNo)
{
fstream File;
File.open (“STOCK.DAT”, ios ::binary | ios :: in | ios :: out);
cin>> Ino;
cin.getline(Itm,20 );
_________________________ //Statement 2
File.write ((char*) this, sizeof(Item ));
File.close ( );
}
(b) Write a program to create a text file “ TEXT.DOC “ . Tranfer the lines that start with a vowel ( not case
sensitive ) to the file in reverse order . ( do not use strrev ) to a new file “EXAM.DOC” . Merge the content of
both the files into a third file “ FINAL.DOC “ , contents of “TEXT.DOC” followed by “ EXAM.DOC “ . Also find the
total number of bytes occupied by the file.
(c) Write a function in C++ to search for BookNo from a binary file “BOOK.DAT”, assuming the binary file is
contained the objects of the following class:
class BOOK
{
int Bno; char Title [20];
public :
int Rbno ( )
{
return Bno;
}
void Enter ( )
{
cin >> Bno; gets (Title);
}
void Display ( ) { cout << Bno <<Title <<endl; }
};
int retdist()
{ return distance; }
};
c. Write a function in C++ that would read the contents of file “BUS.DAT” and display the details of those buses
which travels the distance more than 100 km.
Explain the file opening methods? Give examples.
(d)class PracFile
{
int Pracno;
char PracName[20];
int TimeTaken;
int Marks;
public:
void EnterPrac( ); // function to enter PracFile details
void ShowPrac( ): // function to display PracFile details
int RTime() // function to return TimeTaken
{
return TimeTaken;
10 | Page C++ Mo. No. 9810301034 [email protected]
Computer Science C++ By Gajendra Sir
}
void Assignmarks (int M) // function to assign Marks
{
Marks = M;
}
};
void AllocateMarks( )
{
fstreamFile;
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
______________ //statement 2
Record + + ;
}
File.close();
}
If the function AllocateMarks () is supposed to Allocate Marks for the records in the file MARKS.DAT based on
their value of the member TimeTaken. 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.
Write functions in C++ to read the content from a text file Story.TXT, count and display the number of line are
present in the story.
Given a binary file “BUS.DAT”, containing records of the following class bus type.
class bus
{
int bus_no;
char desc[40];
int distance; //in km
public:
void read()
{
cin>>bus_no; gets(desc) ; cin>>distance;
}
void display()
{
cout<<bus_no; puts(desc); cout<<distance;
}
int retdist()
{
return distance;
}
};
Write a function in C++ that would read the contents of file “BUS.DAT” and display the details of those buses
which travels the distance more than 100 km. Explain the file opening methods? Give examples.
11 | Page C++ Mo. No. 9810301034 [email protected]
Computer Science C++ By Gajendra Sir