4.b) Write a function in C++ to count the number of lowercase alphabets present in a text file “BOOK.TXT”.
Solution:
void LowerLetters( )
{ clrscr( );
ifstream fin("BOOK.TXT",ios::in);
char ch;
int lowercount=0;
while(fin)
{fin.get(ch);
if(islower(ch))
lowercount++;}
cout<<"\nTotal number of Lowercase alphabets in the file = "<<lowercount;
getch( );
}
4.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.
Solution:
void TRANSFER( )
{ ifstream
fin(“PHONE.DAT’,ios::in,ios::binary);
ofstream fout(“PHONEBACK.DAT”,ios::out,ios::binary);
phonlist P;
while(fin) // or while(!fin.eof( ))
{ fin.read((char*)&P,sizeof(P));
if(P.CheckCode(“DEL”)= = 0)
fout.write((char*)&P,sizeof(P));
}
fin.close( );
fout.close( );
}
CBSE Computer Science Solved Revision Tour By Mr. Ravi Kiran ( [email protected] )