COMPUTER SCIENCE DATA FILE HANDLING

4.c) Given a binary file GAME.DAT, containing
records of the following structure type

struct Game
{ char GameName[20] ;
char Participate[10][30] ;
} ;

Write a function in C++ that would read contents from the file GAME.DAT and creates a file named BASKET.DAT copying only those records from GAME.DAT where the game name is “Basket Ball”.

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( );
}

OUTSIDE DELHI : 2007

4.a) Observe the program segment given below carefully,and answer the question that follows:

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 ;
int Rec=0 ;
while (File.read ( (char*) &L,sizeof (L) ) )
{ if (L.RChecked( )= =’N’)
L.Assignmarks (0)
else
L.Assignmarks (10)
File.seekp(File.tellp( )-
sizeof(L)); //Statement 1
//File.seekp(Rec*sizeof(L
));
File.write((char*)&L,sizeof(L));
//Statement 2
//File.write((char*)&L,siz
eof(Labrecord));
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 ppropriate place in the file and statement 2 is to perform the write operation with the modified record.

 

CBSE Computer Science Solved Revision Tour By Mr. Ravi Kiran ( [email protected] )