Page 186 - C++
P. 186
if(!fout)
{
cout<<”File cannot be opened “;
return 0;
}
while (str[i]!=’\0’)
{
fout<<str[i];
i++;
}
fout.close();
}
OR
(a) Write a function showfile() to read all the records present in an already exiting binary file SPEED.DAT and
display them on the screen ,also count the number of records present in the file. (2)
Ans.
void showfile()
{ ifstream fin;
fin.open(“SPEED.DAT”,ios::in|ios::binary);
vehicle v1;
int count=0;
while (!fin.eof())
{
fin.read((char *)&v1,sizeof(v1));
count++;
v1.showdetails();
}
cout<<”Total number of records are: “<<count;
}
(b) Assuming the class VINTAGE as declared below, write a function in C++ to read the objects of VINTAGE
from binary file “VINTAGE.DAT” and display those vintage vehicles, which are priced between 200000 and
250000. (3)
class VINTAGE
{
int VNO; //Vehicle Number