Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

File Handling: Count For His:2 Count For Her:1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

FILE HANDLING

1. WAF to copy the words not starting with vowel and second character as ‘F’ in a text file “PARA.TXT” from
“VOWEL .TXT”.
2. WAF in C++ to read a file “SPACE.DAT” and copy all the content into file “DOUBLE.DAT” with all the
sequence of consecutive blank spaces must be replaced by single space.
3. Write a function CountHisHer ( ) in C++ which reads the contents of a text file diary.txt and counts the
words His and Her (not case sensitive). For example, if the file contains.
Pinky has gone to his friend's house. His friend's name is Ravya. Her house is 12 KM from here.
The function should display the output as Count for His:2 Count for Her:1
4. Given a binary file ‘‘SPORTS.DAT’’ containing records of the following class:
class Player { char PNO[10]; //player number
char Name[20]; //Name of player
int rank; //rank of the player public:

void EnterData() { gets(PNO);gets(Name);cin>>rank; }


void DisplayData() { cout<<setw(12)<<PNO; cout<<setw(32)<<Name;
cout<<setw(3)<<rank<<endl; }
int Ret_rank() {return rank; } };
Write a function in C++ that would read contents of the file ‘‘SPORTS.DAT’’ and display the details of
those players whose rank is above 500.
5. A binary file ‘‘games.dat’’ contains data of 10 games where each game’s data is an object of the following
class: class game { int gameno;
char game_name[20];
public:
void enterdetails() {cin>>gameno; gets(game_name);}
void enterdetails() {cout<<gameno<<endl<<game_name;} };
With reference to this information, write C++ statement in the blank given below to move the file pointer to
the end of file.
ifstream ifile;
game G;
ifile.open("games.dat",ios::binary|ios::in);
___________________________________
cout<<ifile.tellg();
6. Write a function EUCount ( ) in C++, which should read each character of a text file IMP.TXT, should count
and display the occurrence of alphabets E and U (including small cases e and u too).
Example: If the file content is as follows:
Updated information is simplified by official websites.
The EUCount( ) function should display the output as E:4 U:1
7. 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.
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;} };
8. 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));
CF.read((char*) &C, sizeof(C));
int POS=CF.tellg()/sizeof(C);
cout<<”PRESENT RECORD:”<<POS<<end1;
CF.close(); }
9. Consider a file F containing objects E of class Emp.
a) Write statement to position the file pointer to the end of the file
b) Write statement to return the number of bytes from the beginning of the file to the current position of the
file pointer.
10. 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.
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
11. 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 “ON 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;} };
12. Find the output of the following C++ code considering that the binary file MEM.DAT exists on the hard disk
ith 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(); }
13. Write a function definition ARTICLES() in C++ to count all the articles “the”, “a” and “an” present in a text
file “BOOK.TXT”. Note : Ensure that “the”, “a” and “an” are counted as independent words and not as
a part of any other word. Example : If the following is content in the file BOOK.TXT :
We should choose low fat diet. The chef is good in the hotel. An article came in the newspaper about
him.The function ARTICLE( ) should display the following output 5
14. Write definition of a function CALSAL( ) in C++ to find the total salary paid to all the workers in a
company. The worker’s detail of this company is stored in a binary file WORKERS.DAT.
class WORKER { int WID; char Name[20]; float Salary;
public: void INPUT() { cin>>WID;gets(Name);cin>>Salary; }
void OUTPUT() { cout<<WID<<":"<<Name<<end1;
cout<<Salary<<endl; }
float *GetSal() {return Salary;} };
15. Find the output of the following C++ code considering that the binary file PRODUCT.DAT exists on the
hard disk with a list of data of 350 products.
class PRODUCT { int PCode;char PName[20];
public: void Entry();void Disp(); };
void main() { fstream In;
In.open("PRODUCT.DAT",ios::binary|ios::in);
PRODUCT P;
In.seekg(0,ios::end);
cout<<"Total Count: "<<In.tellg()/sizeof(P)<<endl;
In.seekg(70*sizeof(P));
In.read((char*)&P, sizeof(P));
In.read((char*)&P, sizeof(P));
cout<<"At Product:"<<In.tellg()/sizeof(P) + 1;
In.close(); }
16. Write a user-defined function named Count() that will read the contents of text file named “Report.txt” and
display the count of the number of lines that start with either ‘I’ or ‘M’.
E.g. In the following paragraph, there are 3 lines starting with ‘I’ or ‘M’:
“India is the fastest growing economy. India is looking for more investments around the globe. The
whole world is looking at India as a great market. Most of the Indians can foresee the heights that
India is capable of reaching.”
17. Class item
{ int itmid;
int qty;
float price;
public :
void Newitem ( ) { cin>>itmid >>qty>>price; }
void showitem ( ) { cout<<itmid<<qty<<price; }
void setprice (float P) { price = P; }
itn Ret_id() { return itmidl } };
Write a function named Change_Item(int Id, float Pr) to modify the price of the item whose Itemid & new price are
passed as an argument.
18. Write a function RevText() to read a text file “ Input.txt “ and Print only word starting with ‘I’ in reverse
order . Example: If value in text file is: INDIA IS MY COUNTRY
Output will be: AIDNI SI MY COUNTRY
19. Write a function void Firstmiss( ) in C++ to display every word of a text file “DOCUMENT.TXT” barring
the first alphabet of each word.
For example : if the content of the file is as follows: This is kind of English language.
The function should display the following text his s ind f nglish anguage.
20. 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.
21. Following is the structure of each record in a data file named “CRICKET.DAT”. Write a function in C++ to
update the file with a new value of runs. The Runs, the player_name whose runs have to be updated, are to be
read during the execution of program
struct Cricket { char Player_name[30];
char T_name[20];
int Runs; };
22. Write a function wordcount() in C++ to which reads the contents of a text file “story.txt” and display the
average word length of all the words present In the file.
23. Consider the following class declaration:
class customer { int custno;
char cname[20];
char phone[10];
char address[50];
public: void input() {cin>>custno>>cname>>phone;gets(address);}
void display(){cout<<custno<<” “<<cname<<” “<<phone<<” “<<address;}
void update(char ph[],char add[])
{ strcpy(phone,ph);strcpy(address,add); }
int getcustno( ) { return custno; } };
The file “cust.dat” stores the details of customers . Write a function in C++ for a given customer number
to update the phone number and address without using temporary file.
24. In the binary file STUDENT.DAT as mentioned in question number 4b contains the following records, find
the output of the C++ code written below the content

Name Marks
Rohit 95
Surya 93
Ankit 93
shweta 99
roshni 96

fstream f;
f.open(“STUDENT.DAT”,ios:: binary|| ios::in);
student S;
f.read((char*)&S,sizeof(S));
f.seekg(2*sizeof(S));
S.Show();
f.read((char*)&S,sizeof(S));
cout<<”Rec:”<<F.tellg( )/ sizeof(S)<<endl;
S.Show();
25. Write function definition for show() in C++ to read text file STORY.TXT and display all such words which have the
alphabet i or I in the file.
26. Given the binary file ITEM.DAT, containing the records of the following structure:
class item{ int item_no; char item_name[20];
int stock;
public: int itmreturn() { return item_no;}};
Implement the function DelStock(), which delete a record of matching item_no entered through keyboard.
27. Write a function in C++ to read a text file “ YOURS .TXT “ and display each wordin reverse order.
Example: If value in text file is: Jack and Jill went up the hill
Output will be: kcaj dna llij tnew pu eht llih
28. Assume that a text file named “ALPHA.TXT” already contains some text written into it . But while writing into the file ,
the word “are” has been misspelled “aer” everywhere in the file . Write a function named Rectify () in c++ that reads
the file “ALPHA.TXT” and corrects the word “aer” .
29. Write a function in C++ to read the content of the text file “DELHI.TXT” and display all those lines on screen
which are either starting with ‘D’ or starting with ‘M’.

30. Write a function definition for JTOI( ) in C++ that would display the corrected version of entire content of the file
WORDS.TXT with all the alphabets ‘J’ to be displayed as an alphabet ‘I’ on screen.
31. Write a function in C++ to search for a room number (R_no) given by the user in an already existing binary file
“HOSPITAL.DAT”. Also display complete record of the R_no searched if found otherwise display a message “Not
found”.
class hospital { int R_no;
char P_name[30];
public :
void input( ) { gets (P_name); cin>>R_no; }
void output( ) { cout<<”Patient’s Name”<<P_nname<<endl;
cout<<”Room Number”<<R_no<<endl; }
intGetroom() {returnR_no; } };
32. Given a binary file EMPLOYEE.DAT containing records of the following type :
class Emp { char Ename[20], status ;
int Eid ;
public: void read();
void show();
char *getstatus() { return status ; }
void setstatus(char s) { status = s; }
int getid() { return id; } };
Write a function which changes the status of all Employee with previous status as ‘A’ to ‘B’ .
33.Write a function in C++ to search and display details, whose destination is “Chandigarh” from binary
file “Flight.Data”. Assuming the binary file is containing the objects of the following class:
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 input() { cin>>Fno>>; gets(From); get(To); }
void show( ) { cout<<Fno<< “:”<<From << “:” <<To<<endl; } };

You might also like