Вы находитесь на странице: 1из 14

Computer Science C++ By Gajendra Sir

Set 1 , Question 4 (MM: 6) [Data File]


4. (a) Observe the program segment given below carefully and answer the question that follows:
# include<fstream.h>
class Book
{
intBno ;
char Title [20] ;
public :
void EnterVal ()
{
cin>>Bno ; cin.getline ( Title, 20);
}
void showVal ()
{
cout<<Bno<< “#” << Title << end l ;
}
};
void Search ( intRecNo)
{
fstream File ;Book B ;
File. open ( “BOOK.DAT”, ios : : binary | ios : : in) ;
_________________ // Statement 1
File. read (( char*) &B, sizeof (B) ) ;
B.Show();
File.close();
}
void Modify(intRecNo)
{
fstreamFile;Book B;
File.open(“BOOK.DAT”,ios::binary| ios::in |ios :out);
B.EnterVal( );
___________________ //Statement 2
File.write((char *)&B,sizeof(B));

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 g9_sharma@yahoo.com
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 g9_sharma@yahoo.com
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 g9_sharma@yahoo.com
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”,

Set 4 , Question 4 (MM: 6) [Data File]


Q4: a) Observe the program segment given below carefully, and answer the question that follows
class Candidate
{
long CId ; //Candidate’s Id
char CName[20]; // Candidate’s Name float Marks;
//Candidate’s 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. MarkChange ( );
Found = 1 ;
}

// Statement 1
// Statement 2

Record ++ ;
4 | Page C++ Mo. No. 9810301034 g9_sharma@yahoo.com
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.

Set 5, Question 4 (MM: 6) [Data File]


4(a) Observe the program segment given below carefully and fill the blanks marked in statement 1 using
seekg( ) or seekp( ) functions for performing the required task.
#include<fstream.h>
class FILE
{
int Num;
char Name[30];
public:
void GO_Record(int); }; //function to read Nth record from the file

void FILE::GO_Record(int N)
{

FILE Rec; Fstream File;


File.open(“STOCK”,ios::binary|ios::in);
//statement 1
File.read((char*)&Rec,sizeof(Rec));
cout<<Rec.Num<<Rec.Name<<endl;
File.close( );
}
(b)Write a function to count and print the number of complete words as “to” and “are” stored in a text file
“ESSAY.TXT”.
void CWORDS( )
{
ifstream fin(“ESSAY.TXT”);
5 | Page C++ Mo. No. 9810301034 g9_sharma@yahoo.com
Computer Science C++ By Gajendra Sir
char st[80];
int count=0; while(!fin.eof())
{
fin>>st;
if(!fin)
break;
if(strcmpi(st,”to”) = =0 || strcmpi(st,”are”)= =0)
count++;
}
cout<<”\nTotal ‘to’ & ‘are’ words = “<<count;
fin.close( ); }
(c)Write a function in C++ to display object from the binary file “PRODUCT.Dat” whose product price is more
than Rs 200. Assuming that binary file is containing the objects of the following class:
class PRODUCT
{
int PRODUCT_no;
char PRODUCT_name[20];
float PRODUCT_price;
public:
void enter( )
{
cin>> PRODUCT_no ;
gets(PRODUCT_name) ;
cin >> PRODUCT_price;
}
void display()
{
cout<< PRODUCT_no ;
cout<<PRODUCT_name ;
cout<< PRODUCT_price;
}
int ret_Price( )
{
return PRODUCT_price;
}
};
Set 6, Question 4 (MM: 6) [Data File]
Q4. a. Observe the program segment given below and fill the gaps as staements1 and statement 2 using seekp()
and tellg() functions.
# include<fstream.h>
class employee
{
int eno; char Ename[20];
public:
int counter();
};

int employee : : counter ()


{
fstream File;
File.open(“EMP.DAT”, ios: : binary | ios : : in);
_______________ // statement 1
6 | Page C++ Mo. No. 9810301034 g9_sharma@yahoo.com
Computer Science C++ By Gajendra Sir
int bytes =____________ // statement 2
int count =bytes /sizeof(employee);
File .close();
return count;
}
b. Write a function in C++ to count the words “This” and “These” present in a text file “NOTES.TXT”.

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 g9_sharma@yahoo.com
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; }
};

Set 8, Question 4 (MM: 6) [Data File]


4.(a) Observe the program segment given below carefully, and answer the question that follows:
class Applicant
{
long Aid; //Applicant’s Id
char Name [20] //Applicant’s Name
float Score; //Applicant’s Score
public:
void Enroll();
void Disp();
void MarksScore(); //Function to change score
long R_Aid ()
{
return Aid;
};
void score update(long ID)
{
fstream File ;
8 | Page C++ Mo. No. 9810301034 g9_sharma@yahoo.com
Computer Science C++ By Gajendra Sir
File.open (“APPLI.DAT”, ios::binary | ios:: in | ios:: out);
Applicant A;
int Record=0,Found=0 ;
while(! Found && File.read ((char*)&A, sizeof(c)))
{
If( Id==A.R_AID())
{ cout<< “enter new score”;
A.MarkScore();
________ //Statement 1
________ //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 Applicant’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 lines present in a text file
"STORY.TXT".
(c) Given a binary file PHONE.DAT, containing records of the following structure type.
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) ;
}
};
Write a function TRANSFER () in C++, that would copy all those records which are having AreaCode as “DEL”
from PHONE.DAT to PHONBACK.DAT.

Set 9, Question 4 (MM: 6) [Data File]


Q.4(a) 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; } };
9 | Page C++ Mo. No. 9810301034 g9_sharma@yahoo.com
Computer Science C++ By Gajendra Sir
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.
b. 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; }
};
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 g9_sharma@yahoo.com
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 g9_sharma@yahoo.com
Computer Science C++ By Gajendra Sir

Set 10, Question 4 (MM: 6) [Data File]


4 a)Consider the following declaration:
class TRAIN
{
int trainno;
char dest[20];
float distance;
public:
void get( ); //To read an object from the keyboard
void put( ); //To write an object into a file
void show( ); //To display the file contents on the monitor
};
Complete the member functions definitions
(b) Assuming the class COLLEGE,write a function in C++ to perform the following.
(i)Write the objects of COLLEGE to a binary file.
(ii)Reads the objects of COLLEGE from binary file and display them on the screen.
class COLLEGE
{
char name[20];
char place[20];
public:
void getdata()
{
cin>>name;
cin>>place;
}
void display()
{
cout<<name;
cout<<place;
}
};
(C). Write a user defined function in C++ to read the contents from a text file HAI.TXT, count and display the
number of alphabets present in it.
(d)Assuming a binary file jokes.dat is containing objects belonging to a class Joke(as defined below).Write a
user defined function in C++ to add more objects belonging to class Joke at the botton of it
class JOKE
{
int Jokeid;
char Type[5];
public:
void getdata()
{
cin>> Jokeid;
cin>> Type;
}
void display()
{
cout<< Jokeid;
cout<< Type;
}
12 | Page C++ Mo. No. 9810301034 g9_sharma@yahoo.com
Computer Science C++ By Gajendra Sir
};
Set 11, Question 4 (MM: 6) [Data File]
4. (a) Differentiate between read() and write() functions.
(b) Write a function in C++ to count and display the number of lines starting with alphabet ‘S’ present in a text
file “STORY.TXT”.
(c) Given a binary file APPLY.DAT, containing records of the following class applicant type
Class applicant
{
char a_rno[10];
char a_name[30];
int a_score;
public:
void enroll()
{
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 applicants
whose a_score is in between 75 and 90.

Set 12, Question 4 (MM: 6) [Data File]


4.(a) Observe the program segment given below carefully and fill in the blanks marked as Statement 1 and
Statement 2 for performing the required task.
#include <iostream.h>
#include <fstream.h>
void main(void)
{
char filename[] = "C:\\testfileio3.txt";
fstream inputfile, outputfile;
int length;
char * buffer; // --------create, open and write data to file--------
outputfile.open(filename, ios::out); // ----write some text-------
outputfile<<"This is just line of text."<<endl; // --------close the output file------------
outputfile.close(); // ----opening and reading data from file-----
inputfile.open(filename, ios::in);
cout<<"The "<<filename<<" file was opened successfully!\n";
cout<<"\nMove the pointer to the end\n" <<"Then back to the beginning with\n" <<"10 offset. The pointer
now at...\n"<<endl; // flush the stream buffer explicitly...
cout<<flush; // get length of file move the get pointer to the end of the stream
inputfile.seekg(0, ios::end); // This statement returns the current stream position.
13 | Page C++ Mo. No. 9810301034 g9_sharma@yahoo.com
Computer Science C++ By Gajendra Sir
length = _____________________________ //Statement1
cout<<"length variable = "<<length<<"\n"; // dynamically allocate some memory storage for type char...
buffer = new char [length]; // move back the pointer to the beginning with offset of 10
____________________________________ //Statement2
// read data as block from input file...
inputfile.read(buffer, length);
cout<<buffer;
// free up the allocated memory storage...
delete [] buffer;
inputfile.close();
}
(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..
Example:
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 are 16
(c) Given a binary file “TABLE.TXT”, containing the records of the following class type
class perdata
{ int age;
int weight;
int height;
char name[40];
public:
void getdata() { cin>>age>>weight>>height>>name; }
void showdata() { cout<<age<<” “<<weight<<” “<<height<<” “<<name<<endl; }
int retage()
{
return age;
}
};
Write a function in c++ that would read contents from the file personal.dat and creates a file named eligible.dat
copying only those records from personal.dat having age >= 18.

14 | Page C++ Mo. No. 9810301034 g9_sharma@yahoo.com

Вам также может понравиться