Sei sulla pagina 1di 9

BOARDQUESTIONS(2Marks)

1. WriteafunctioninC++tocountanddisplaythenumberof
lines starting with alphabet A present in a text file
LINES.TXT.
Example:
IfthefileLINES.TXTcontainsthefollowinglines,
Aboyisplayingthere.
Thereisaplayground.
Anaeroplaneisinthesky.
Alphabetsandnumbersareallowedinthepassword.
Thefunctionshoulddisplaytheoutputas3
Ans:voidcounter()
{
charS[80];
intCount=0;
ifstreamFin(LINES.TXT);
while(Fin.getline(S,80))
{
if(S[0]==A)
Count++;
}
Fin.close();
cout<<Count<<endl;
}
2. WriteafunctioninC++tocountanddisplaythenumberof
linesnotstartingwithalphabetApresentinatextfile
STORY.TXT.
Example:

IfthefileSTORY.TXTcontainsthefollowinglines,
Theroseisred.
Agirlisplayingthere.
Thereisaplayground.
Anaeroplaneisinthesky.
Numbersarenotallowedinthepassword.
Thefunctionshoulddisplaytheoutputas3
Ans:voidCOUNTALINES()
{
ifstreamFILE(STORY.TXT);
intCA=0;
charS[80];
while(FILE.getline(S,80))
{
if(S[0]!=A)
count++;
}
cout<<NotStartingwithAcountsto<<count<<endl;
FILE.close();//Ignore
}
A.ShortAnswerTypeQuestions:(3Marks)
1. GivenabinaryfileSTUDENT.DAT,containingrecordsof
thefollowingclassStudenttype.
classStudent
{
charS_Admno[lO];//Admissionnumberofstudent

charS_Name[30];//Nameofstudent
intPercentage;//MarksPercentageofstudent
public:
voidEnterData()
{
gets(S_Admno);gets(S_Name);cin>>Percentage;
}
voidDisplayData()
{
cout<<setw(12)<<S_Admno;
cout<<setw(32)<<S_Name;
cout<<setw(3)<<Percentage<<endl;
}
intReturnPercentage(){returnPercentage;}
};
2. WriteafunctioninC++,thatwouldreadcontentsoffile
STUDENT.DATanddisplaythedetailsofthoseStudents
whosePercentageisabove75.
Ans:voidDistinction()
{
StudentS;
fstreamFin;
Fin.open(STUDENT.DAT,ios::binary|ios::in);
while(Fin.read((char*)&S,sizeof(Student))
{
if(S.ReturnPercentage()>75)
S.DisplayData();

}
Fin.close();
}

2.GivenabinaryfileAPPLY.DAT,containingrecordsofthe
followingclassApplicanttype
classApplicant
{
charA_Rno[10];//Rollnumberofapplicant
charA_Name[30];//Nameofapplicant
intA_Score;//Scoreofapplicant
public:
voidEnrol()
{
gets(A_Rno);
gets(A_Name);
cin>>A_Score;
}
voidStatus()
{
cout<<setw(12)<<A_Admno;
cout<<setw(32)<<A_Name;
cout<<setw(3)<<A_Score<<endl;
}
intReturnScore()
{
returnA_Score;}
};
WriteafunctioninC++,thatwouldreadcontentsoffile
APPLY.DAT and display the details of those Students
whoseA_Scoreisbelow70.

Ans:voidREADAPPLY()//Ignore
{
fstreamFILE;
FILE.open(APPLY.DAT,ios::binary|ios::in);
ApplicantA;
while(FILE.read((char*)&A,sizeof(A)))
{
if(A.ReturnScore()<70)
A.Status();
}
FILE.close();//Ignore
}
STRUCTURES
3.GivenabinaryfileSPORTS.DAT,containingrecordsofthe
followingstructuretype:
structSports
{
charEvent[20];
charParticipant[10][30];
};
WriteafunctioninC++thatwouldreadcontentsfromthe
file SPORTS.DAT and creates a file named
ATHLETIC.DAT copying only those records from
SPORTS.DATwheretheeventnameisAthletics.

Ans: //Function to copy records from SPORTS.DAT to


ATHELETIC.DAT
voidSP2AT()
{
fstreamIS,OA;
SportsS;
IS.open(SPORTS.DAT,ios::binary|ios::in);
OA.open(ATHLETIC.DAT,ios::binary|ios::out);
while(IS.read((char*)&S,sizeof(S)))
{
if(strcmp(S.Event,Athletics)==0)
OA.write((char*)&S,sizeof(S));
}
IS.close();
OA.close();
}

4.GivenabinaryfileGAME.DAT,containingrecordsofthe
followingstructuretype
structGame
{
charGameName[20];
charParticipant[10][30];
};
WriteafunctioninC++thatwouldreadcontentsfromthe
fileGAME.DATandcreatesafilenamedBASKET.DAT
copyingonlythoserecordsfromGAME.DATwherethe
gamenameisBasketBall
Ans:voidCopyBasket()
{
GameG;
ifstreamfin;
fin.open(GAME.DAT,ios::binary);
ofstreamfout;
fout.open(BASKET.DAT,ios::binary);
while(fin.read((char*)&G,sizeof(G)))
{
if(strcmp(G.GameName,BasketBall)==0)
fout.write((char*)&G,sizeof(G));
}
fin.close();//ignore
fout.close();//ignore
}

Potrebbero piacerti anche