COMPUTER SCIENCE DATA FILE HANDLING

4.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.

Solution:

void COPYABC( )
{ ifstream fin(“TELEPHON.DAT’,ios::in|
ios::binary);
ofstream fout(“TELEBACK.DAT”,ios::out,ios|binary);
Directory D;
while(fin) // or while(!fin.eof( ))
{ fin.read((char*)&D,sizeof(D));
if(D.CheckCode(“123”)= = 0)
fout.write((char*)&D,sizeof(D));
}
fin.close( );
fout.close( );

DELHI : 2008

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 ScoreUpdate (long Id)
{ fstream File ;
File.open (“APPLI.DAT” , ios :: binary l ios :: in l ios :: out) ;
Applicant A ;
int Record = 0, Found = 0 ;
while (!Found && File.read( (char*)&C, sizeof(c) ) )
{ if (Id = = A.R_Aid( ) )
{ cout << “Enter new Score” ;
A.MarksScore( ) ;
File.seekp(File.tellp( )-sizeof(A));
//Statement 1
//File.seekp(Record*size
of(Applicant));
File.write((char*)&A,sizeof(A));//Statement2
Found=1;
}
Record++ ;
}
if (Found = = 1)
cout << “Record Updated” ;
File.close( ) ;
}

Write the Statement1 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.

 

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