4.b) Write a function in C++ to print the count
of the word the as an independent word in a
text file STORY.TXT.
For example,if the content of the file STORY.TXT is There was a monkey in the zoo.The monkey was very naughty. Then the output of the program should be .
2
Solution:Dear Children, try this answer.
4.c) Given a binary file SPORTS.DAT,containg 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 “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( );
}
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 (Since, the file is opened in out mode, it
looses all the previous content, if the file mode is app, then result will be 4)
CBSE Computer Science Solved Revision Tour By Mr. Ravi Kiran ( [email protected] )