Since the class objects are the central elements of C++ programming, so the language support features for writing to and reading from the disk files object directly. The binary input and output functions read() and write() are designed to do the given task.

Code:
#include< fstream.h>
#include< iomanip.h>
#include< conio.h>
class detail
{
char name[10];
int code;
float cost;
public:
void readdata(void);
void writedata(void);
};
void detail:: readdata(void)//read from keyboard
{
cout<<"\nEnter name: ";
cin>>name;
cout<<"\nEnter code: ";
cin>>code;
cout<<"\nEnter cost: ";
cin>>cost;
}
void detail::writedata(void)//formatted display on the screen
{
cout<< setiosflags(ios::left)<< setw(10)<< name
<< setiosflags(ios::right)<< setw(10)<< code
<< setprecision(2)<< setw(10)<< cost
<< endl;
}
main()
{
clrscr();
detail item[3];//Declare array of3 objects
fstream file; //Input and Output file
file.open("details.dat",ios::in,ios::out);
cout<<" Enter Details For Three Items \n";
for(inti=0;i<3;i++)
{
item[i].readdata();
file.write((char*) &item[i],sizeof(item[i]));
}
file.seekg(0);//reset to start
cout<< "\nOUTPUT\n\n";
for(i=0;i<3;i++)
{
file.read((char*) &item[i], sizeof(item[i]));
item[i].writedata();
}
file.close();
}
The output of the program :

Enter name:xyz
Enter code:121
Enter cost:500
Enter name:mmm
Enter code:222
Enter cost:600
Enter name:nnn
Enter code:333
Enter cost:400


OUTPUT


xyz 121 500
mmm 222 600
nnn 333 400